Skip to content Skip to sidebar Skip to footer

ListView Gets Empty When Back Icon Is Clicked

I have two activities, in the main one I have a text field and two buttons, the user can enter text in the text field. When clicking the add button,text will be added to some Array

Solution 1:

create a class Constantss.java and declare

public static ArrayList <String> itemList=null;

then inside your class

use

Constantss. itemList.add(name);

instead of

itemList.add(name);

// replace itemList with Constantss. itemList in all of your classes


Solution 2:

I believe this is a navigation issue. So you should close your ListActivity like this:

public class ListActivity extends ActionBarActivity {

    //your code

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        int itemID = item.getItemId();

        switch (itemID ) {
            case android.R.id.home:
                this.finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

Anyway your data can be lost if android destroys your MainActivity. So if you navigate from ListActivity back to MainActivity, MainActivity will be recreated and you data get lost. So you should find the way how to save your data permanent.

there are two common ways:

  1. Save data to data base (SQLite)
  2. Save data to Preferences How to store list of values in SharedPreferences

Post a Comment for "ListView Gets Empty When Back Icon Is Clicked"