Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to maintain the previous state of an activity

I am having two activities (activity1 and activiy2) and each activity is having one button each. In activity1 i having a spinner with few options. Suppose i am selecting option 2 from this spinner and i am clicking the button in activity1, then activity2 starts. When i click back button activity1 is resumed and the same option 2 is visible (like i need). Now the problem is that if my activity2 is started and i am clicking a button in it, activity1 is started. But instead of resuming the previous state of activity1 it starts in a way that it has just created and the previous selection is changed. How can i get the same facility like back button (not the facility of going back to previous activity, i mean automatically resuming the previous state of any activity) even when i start the activity again. Simply i need to know how to maintain the previous state of an activity if it is again visited.

It is with this code I go from one activity to another when button is clicked:

Intent intent=new Intent();
intent.setClassName(getApplicationContext(),"com.myapp.activityname");

startActivity(intent);

Kindly help me.I am a beginner in android, so if any one is giving the answer please explain it a bit. Thanks in adavnce

like image 594
roy mathew Avatar asked Feb 05 '12 08:02

roy mathew


People also ask

What code would you use to go back to a previous activity?

Android activities are stored in the activity stack. Going back to a previous activity could mean two things. You opened the new activity from another activity with startActivityForResult. In that case you can just call the finishActivity() function from your code and it'll take you back to the previous activity.

What are the four essential states of an activity?

Active: If the activity is in the foreground that means it is running on the device. Paused: When the activity is at the background and is still visible on the screen. Stopped: when the activity is not visible and is actually hidden by another activity that is running on the device.

What allows you to properly restore a user state when an activity is restarted?

State can be restored in either the onCreate() or the onRestoreInstanceState() methods of the activity by extracting values from the Bundle object and updating the activity based on the stored values.


2 Answers

Think I found the answer. Let me tell what I have done in simple words,

Suppose i am having two activities activity1 and activity2 and i am navigating from activity1 to activity2(i have done some works in activity2) and again back to activity 1 by clicking on a button in activity1. Now at this stage I wanted to go back to activity2 and i want to see my activity2 in the same condition when I last left activity2.

For the above scenario what i have done is that in the manifest i made some changes like this:

<activity android:name=".activity2"
          android:alwaysRetainTaskState="true"
          android:launchMode="singleInstance">
</activity>

And in the activity1 on the button click event i have done like this:

Intent intent=new Intent();
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
intent.setClassName(this,"com.mainscreen.activity2");
startActivity(intent);

And in activity2 on button click event i have done like this:

Intent intent=new Intent();
intent.setClassName(this,"com.mainscreen.activity1");
startActivity(intent);

Now what will happen is that whatever the changes we have made in the activity2 will not be lost, and we can view activity2 in the same state as we left previously.

I believe this is the answer and this works fine for me.

like image 190
roy mathew Avatar answered Nov 15 '22 09:11

roy mathew


By overriding an Activity’s onSaveInstanceState event handler, you can use its Bundle parameter to save instance values. Here is an example:

@Override
public void onSaveInstanceState(Bundle outState) {
    // Retrieve the View
    TextView myTextView = (TextView)findViewById(R.id.myTextView);
    // Save its state
    outState.putString("My text",
    myTextView.getText().toString());
    super.onSaveInstanceState(outState);
}

The saved Bundle is passed into the onRestoreInstanceState and onCreate methods if the application is forced to restart during a session. You can then extract values from the Bundle and use them to update the Activity instance state. Here is an example:

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main);
    TextView myTextView = (TextView)findViewById(R.id.myTextView);
    String text = “”;    
    if (icicle != null && icicle.containsKey("My text")) {
        text = icicle.getString(TEXTVIEW_STATE_KEY);
    }
    myTextView.setText(text);
}

It’s important to remember that onSaveInstanceState is called only when an Activity becomes inactive, but not when it is being closed by a call to finish or by the user pressing the Back button.

like image 43
Shashank Kadne Avatar answered Nov 15 '22 08:11

Shashank Kadne