I have my necessary code in OnResume() and not OnCreate(), but array data in ListView still disappears when I go to a different activity then back

Ang :

As the title says, my listview disappears when I leave the activity then return to it. I searched other questions and saw that I had a problem because everything was in OnCreate(). I moved it to OnResume(), but absolutely nothing changed with my app.

I deleted some irrelevant things because it said I had too much code, so if some brackets are messed up, that's why. I'm not getting any bracket errors in AndroidStudio.

Here's my code:

public class Income extends MainActivity {

    EditText enterIncomeEditText;
    EditText enterIncomeNamesEditText;
    Button addIncomeButton;
    ListView incomeAmountListView;
    ListView incomeNameListView;

    // new String Array for listview
    private String[] incomeArray;
    //create getter
    public String[] getIncomeArray() {
        return  incomeArray;
    }

    //new string array for listview names
    private String[] incomeNames;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_income);

        //initialize new String array
        incomeArray = new String[]{
                ""
        };

        //initialize new String Array for listview
        incomeNames = new String[]{
                ""
        };

        //connect EditTexts
        enterIncomeEditText = findViewById(R.id.enter_income_edit_text);
        enterIncomeNamesEditText = findViewById(R.id.enter_income_names_edit_text);
        addIncomeButton = findViewById(R.id.add_income_button);
        incomeAmountListView = findViewById(R.id.income_amount_list_view);
        incomeNameListView = findViewById(R.id.income_name_list_view);
    }

    //override onResume so things don't get deleted each time the activity changes
    @Override
    protected void onResume() {
        super.onResume();

        // Create a List from String Array elements
        final List<String> income_list = new ArrayList<>(Arrays.asList(incomeArray));

        // Create an ArrayAdapter from List
        final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>
                (this, android.R.layout.simple_list_item_1, income_list);

        //Bind ListView with ArrayAdapter items
        incomeAmountListView.setAdapter(arrayAdapter);


        // Create a List from String Array elements
        final List<String> income_names_list = new ArrayList<>(Arrays.asList(incomeNames));

        // Create an ArrayAdapter from List
        final ArrayAdapter<String> arrayAdapterNames = new ArrayAdapter<>
                (this, android.R.layout.simple_list_item_1, income_names_list);

        //Bind ListView with ArrayAdapter items
        incomeNameListView.setAdapter(arrayAdapterNames);

        //add everything for navigation drawer
        mDrawerLayout = findViewById(R.id.drawer_layout);

        addIncomeButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if (TextUtils.isEmpty(enterIncomeEditText.getText()) ||
                        TextUtils.isEmpty(enterIncomeNamesEditText.getText())) {
                    Toast.makeText(Income.this, "Income Name or Amount Empty", Toast.LENGTH_SHORT).show();
                } else {
                    String income = enterIncomeEditText.getText().toString() + "\n";
                    String incomeName = enterIncomeNamesEditText.getText().toString() + "\n";
                    //add new expenses to list and notify data set changed
                    income_list.add(income);
                    income_names_list.add(incomeName);

                    arrayAdapter.notifyDataSetChanged();
                    arrayAdapterNames.notifyDataSetChanged();
                }
                enterIncomeNamesEditText.setText("");
                enterIncomeEditText.setText("");
        });
    }
}
Pranay Bhalerao :

To retain your data on current activity when you transact to another activity, you need to override

public void onSaveInstanceState(Bundle outState)

method in your activity, save all your data in the bundle. So that your data stays in your bundle if you go to another activity, When you press back, then you can retrieve your data with one more method

public void onRestoreInstanceState(Bundle savedInstanceState) 

You can then get your data back from this bundle, and display it in your activity.

Hope this will help you.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=195433&siteId=1