2

I have an Activity where names are inputed via a EditText box and then they get saved into an Array and then displayed in a ListView where there is also a Button that will appear where they can remove it.

Once the correct names have been added they are then passed to the next Activity by a Button Intent to populate a String. On the next Activity there is a button where they can go back to the previous Activity to add more names or remove.

The problem is when they go back to the previous Activity the Array is empty so I need a way to save the Array even when the Activity has been left.

ArrayList<String> playerList = new ArrayList<String>();

Button to add to the Array is below:

Button confirm = (Button) findViewById(R.id.add);
confirm.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        EditText playername = (EditText) findViewById(R.id.userinput);
        playerList.add(playername.getText().toString());
        adapter.notifyDataSetChanged();
        playername.setText("");
    }
});
izb
  • 50,101
  • 39
  • 117
  • 168
Matt
  • 1,747
  • 8
  • 33
  • 58

2 Answers2

4

Use Bundle to save your array on onSaveInstanceState(Bundle savedInstanceState) of activity

  1. http://developer.android.com/reference/android/os/Bundle.html
  2. Saving Android Activity state using Save Instance State

Update

    @Override
public void onSaveInstanceState(Bundle outState) {
  // Save UI state changes to the savedInstanceState.
  // This bundle will be passed to onCreate if the process is
  // killed and restarted.
    outState.putStringArrayList("Playlist", playerList );

   super.onSaveInstanceState(outState);

    }

Now to take value

    @Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
  super.onRestoreInstanceState(savedInstanceState);
  // Restore UI state from the savedInstanceState.
  // This bundle has also been passed to onCreate.
    playerList = savedInstanceState.getStringArrayList("Playlist");

}
Community
  • 1
  • 1
sonu thomas
  • 2,161
  • 4
  • 24
  • 38
2

An alternative to using a Bundle (which is not always flexible enough) is to use a custom Application class. You need to define it in your manifest (application name)

<application android:name="<package>.MyApplication" ..... />

Write the custom class like that:

public class MyApplication extends Application {
    private List<String> data;

    public void storeData(List<String> data) {
        this.data = data;
    }

    public List<String> getData() {
        return data;
    }
}

The good thing with this way is that it allows you all kind of customisation on methods, type of data you can store, etc. Also, you don't need to pass the data between activities, it will always be available and will never be lost as long as the application is active.

From any activity, you can save your data using:

final List<String> myData = new ArrayList<String>();
// ... populate myData ...
((MyApplication) getApplication()).storeData(myData);

And when you need it:

((MyApplication) getApplication()).getData();

In fact, I usually put in my custom application everything I want to be able to access from multiple activities.

Guillaume
  • 22,694
  • 14
  • 56
  • 70