简体   繁体   中英

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

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.

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.

Download source code or see the entire post

For activity one, you need to use the startActivityForResult() function that returns values to the parent activity which is the activity one.

    Intent intent = new Intent(MainActivity.this, Activity2.class);
   startActivityForResult(intent, 1);

And to receive the values from activity two without alteration of activity one, add the following code

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if (requestCode == 1) {
                if(resultCode == RESULT_OK){
                    String text=data.getStringExtra("text");
                    vTxt.setText("Name 1: "+eTxt.getText()+ "\n Name 2: "+text);
                }
            }
        }

Activity 2

add the following code to return to activity one and pass some values

         Intent intent=new Intent(Activity2.this, MainActivity.class);

           Bundle extra=new Bundle();

           extra.putString("text", eTxt.getText().toString());
           intent.putExtras(extra);
           setResult(RESULT_OK, intent);
           finish();

Finally you are done

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM