3

Suppose I have two activities, activity1 and activity2. I want to navigate from activity1 to activity2, get some info from activity2 and insert back it to activity1 and also I don't want to lose activity1 previous state as I left. how can I save its state ?

hasanghaforian
  • 13,858
  • 11
  • 76
  • 167
Obtice
  • 1,205
  • 3
  • 18
  • 44

5 Answers5

4

what you are describing is the perfect classic reason to use the Activity.startActivityForResult() method.

this what google wrote in this method documentation:

Launch an activity for which you would like a result when it finished. When this activity exits, your onActivityResult() method will be called with the given requestCode

so what you should do is: from your activity1 start activity for result, and from activity2 use the setResult(int resultCode, Intent data) method with the data you want your activity1 to get back, and call finish() (it will get back to onActivityResult() in the same state activity1 was before..).

Tal Kanel
  • 10,475
  • 10
  • 60
  • 98
2

Override onSaveInstanceState(Bundle) in activity1 to save whatever data you want then override onRestoreInstanceState(Bundle) in the same activity to get the values back. Using Bundle, you can store pretty much any data that you want. I'd recommend something like this:

public class MainActivity extends Activity {
    ...
    public static final String DATA1_KEY = "data1";

    private boolean value1;
    ...

    @Override 
    protected void onSaveInstanceState (Bundle outState) {
        outState.putBoolean(DATA1_KEY, value1);
    }

    @Override
    protected void onRestoreInstanceState (Bundle savedInstanceState) {
        value1 = savedInstanceState.getBoolean(DATA1_KEY);
    }
}
Mohit Deshpande
  • 53,877
  • 76
  • 193
  • 251
0

You should override onSaveInstanceState(Bundle savedInstanceState)

Please check this answer for example code

Or use SharedPreferences. Check this code

Community
  • 1
  • 1
Jovan
  • 4,684
  • 13
  • 64
  • 98
0

If you want to keep your data alive only at runtime, the consider using static members. Then you can access and manipulate these members from any activites. for example:

public class FirstActivity extends Activity
{
    public static String data = null;

    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(...);
        data = "This is a test!";
    }
}

From your second activity you can access these static variables like

public class SecondActivity extends Activity
{
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(...);
        if(FirstActivity.data!=null)
        {
            //You can use it:
            System.out.println(FirstActivity.data);
        }
    }
}

Of course you can add getter/setter functions to make it safer and more elegant.

If you want to store them for a longer time, the please consider using:

Balázs Édes
  • 13,452
  • 6
  • 54
  • 89
0

The user performs an action in your app that starts a new activity. The current activity is stopped when the second activity is created. If the user then presses the Back button, the first activity is restarted.

When your activity is stopped, the Activity object is kept resident in memory and is recalled when the activity resumes. You don’t need to re-initialize components that were created during any of the callback methods leading up to the Resumed state. The system also keeps track of the current state for each View in the layout, so if the user entered text into an EditText widget, that content is retained so you don't need to save and restore it.

Note: Even if the system destroys your activity while it's stopped, it still retains the state of the View objects (such as text in an EditText) in a Bundle (a blob of key-value pairs) and restores them if the user navigates back to the same instance of the activity.
CommonsWare here says:

When user "press the BACK button", then the Bundle from onSaveInstanceState() (if any) is discarded, as the user has indicated they are finished with the activity. The onSaveInstanceState() Bundle is used in cases where the user has not said they are finished with the activity (e.g., they accepted an incoming phone call) yet Android elects to destroy the activity to free up RAM.

And documentation says:

Note that it is important to save persistent data in onPause() instead of onSaveInstanceState(Bundle) because the later is not part of the lifecycle callbacks, so will not be called in every situation as described in its documentation.

In other words, put your save/restore code for non View objects in onPause() and onResume() instead of onSaveInstanceState(Bundle) and onRestoreInstanceState(Bundle).Finally I guess that you don't need to save any state if you only have View objects and if you have any other states you can use preferences,file or sqlite to save them in onPause() and retreive them in onResume().

You can see more details in these pages:
Stopping and Restarting an Activity
Saving Activity state in Android
Android wont save the current state of an activity

Community
  • 1
  • 1
hasanghaforian
  • 13,858
  • 11
  • 76
  • 167