1

I've got an activity A with one button, after user clicks it, he gets AlertDialog. The result of the dialog (positive, neutral, negative) defines the later UI in the same activity. For example, if user clicks Positive button, he gets a set of 3 new buttons, if neutral then another set of 3 buttons, if negative - nothing happens.

In Activity A I also have NavigationDrawer, each of items in NavigationDrawer opens up a new Activity (Activity B, Activity C, etc.).

The whole app works like this:

  • onCreate Activity A displays Button_1
  • if you click on Button_1 you get an AlertDialog with 2 options
  • if you select Positive Button -> Button_1 hides, and Button_2, Button_3, Button_4 appear
  • if you select Neutral Button -> Button_1 hides, and Button_5, Button_3, Button_4 appear

Button_2 and Button_5 are interchangeable in later stages (i.e. if you click Button_2 it disappears and Button_5 shows up and vice versa).

My main problem is that if I select an item from NavigationDrawer, the applications opens up a new Activity. When I click the back button to move back into Activity A the whole UI gets resetted to the state where Button_1 is on the screen.

What I want to achieve is to remain the set of buttons (no matter what the user clicked in later stages) when we go back from Activities B, C.

How do I save and resume the current State of Activity A after moving back (or closing) from Activity B (C, D, ...)?

Gosia
  • 11
  • 3

1 Answers1

0

Use Bundle to manage Activity states.

For example:

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
  super.onSaveInstanceState(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);
}

to retrieve state use onRestoreInstanceState

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
    // Always call the superclass so it can restore the view hierarchy
    super.onRestoreInstanceState(savedInstanceState);

    // Restore state members from saved instance
    mCurrentScore = savedInstanceState.getInt(STATE_SCORE);
    mCurrentLevel = savedInstanceState.getInt(STATE_LEVEL);
}

https://developer.android.com/training/basics/activity-lifecycle/recreating.html

Also you can use IcePick library to maintain states easily https://github.com/frankiesardo/icepick

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Murat Mustafin
  • 1,284
  • 10
  • 17