Dipen Patel
Dipen Patel

Reputation: 933

Android maintain activity state with data

I have 2 different activities. MainActivity, ContactList & ContactDetails. Now from MainActivity user will tap on Add new and application will open ContactList screen from where user can select any contact to see details which opens ContactDetails activity. Now if user select any no. from ContactDetails, application will go back to MainActivity and add the selected no. in arraylist. I am able to add the data but my problem is that whenever add new record old records are erased. I found the reason that evertime i open MainActivity from ContactDetails it creates new Activity. So i am looking for a way to use OnResume or OnResult Method to solve the problem.

In ContactDetails

Intent intent = new Intent(getBaseContext(), MainActivity.class); intent.putExtra("CONTACT_NO", CONTACT_NO); startActivity(intent)

In MainActivity OnResume method

String data = getIntent().getExtras().getString("keyName");

arr.add(data);

Upvotes: 1

Views: 520

Answers (2)

Aparupa Ghoshal
Aparupa Ghoshal

Reputation: 309

From where ever you are getting the list in MainActivity.java, take it as public static ArrayList or String array and do not initialize it in MainActivity.java.

Also if you have not called finish() for MainActivity (while going to ContactListActivity.java), then add the new data to the arraylist in onResume(). But remember to keep a checking - whether you have a new data to add or not because for the first time, there would be no data to add (as per your posted question).

Upvotes: 0

Marcus
Marcus

Reputation: 6717

If you are looking to persist data during the lifetime of your Activity, SharefPreferences will serve you well. An example:

Write (onPause/onDestroy):

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt(getString(R.string.saved_high_score), newHighScore);
editor.commit();

Read (onResume/onCreate):

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
int defaultValue = getResources().getInteger(R.string.saved_high_score_default);
long highScore = sharedPref.getInt(getString(R.string.saved_high_score), defaultValue);

Read more here.

Update

Alternatively, you can use onSavedInstanceState. Example:

Save state:

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    // Save the user's current game state
    savedInstanceState.putInt(STATE_SCORE, mCurrentScore);
    savedInstanceState.putInt(STATE_LEVEL, mCurrentLevel);

    // Always call the superclass so it can save the view hierarchy state
    super.onSaveInstanceState(savedInstanceState);
}

Restore state:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); // Always call the superclass first

    // Check whether we're recreating a previously destroyed instance
    if (savedInstanceState != null) {
        // Restore value of members from saved state
        mCurrentScore = savedInstanceState.getInt(STATE_SCORE);
        mCurrentLevel = savedInstanceState.getInt(STATE_LEVEL);
    } else {
        // Probably initialize members with default values for a new instance
    }
    ...
}

More information about managing Activity states here.

Upvotes: 0

Related Questions