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("");
    }
});
1
  • send me invitation for chat ASAP i am leaving office in 5 mins Commented Jan 13, 2012 at 13:52

2 Answers 2

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");

}
Sign up to request clarification or add additional context in comments.

2 Comments

Not sure how to implement that can you help me out :)
you want to join me in a chat?
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.

5 Comments

I was about to post the same reply :)
@Matt : This easier way then Saving State of Activity
@MohitSharma that does look a nice way of doing things can one of you join me in a chat?
@Guillaume can you come back into the chat room?
Nope sorry, the chat feature is blocked from my work place :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.