How to keep the previous state of activity

I have two actions (activity1 and activiy2), and each action has one button each. In activity1, I have a counter with several options. Suppose I select option 2 from this counter, and I press the button in action1, then action2 begins. When I press the back button, activity 1 resumes, and the same option 2 is visible (like me). Now the problem is that if my activity2 is running and I press the button on it, action1 starts. But instead of resuming the previous state of activity1, it starts in such a way that it has just been created, and the previous selection has changed. How can I get the same object as the "Back" button (and not the ability to return to a previous activity, I mean automatically resuming the previous state of any activity),even when I start my activity again. I just need to know how to maintain the previous state of activity if it is visited again.

It is with this code that I switch from one activity to another when the button is pressed:

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

startActivity(intent);

Please help me. I am starting to work on android, so if someone gives an answer, please explain this a bit. Thanks at adavnce

+5
source share
3 answers

I think I found the answer. Let me tell you what I did in simple words,

Suppose I have two actions activity1 and activity2, and I move from activity1 to activity2 (I did some work in activity2) and return to activity 1 again by clicking the button in Activity1. Now at this point I wanted to return to activity2, and I want to see my activity2 in the same state the last time I left activity2.

, :

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

1 :

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

Activity2 click :

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

, , , , Activity2, , activity2 , .

, , .

+9

onSaveInstanceState, Bundle . :

@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);
}

onRestoreInstanceState onCreate, . Bundle Activity. :

@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);
}

, onSaveInstanceState , , , finish , "".

+3

Download the source code or see the whole post

For the first action, you need to use the startActivityForResult () function, which returns the values ​​of the parent activity, which is active.

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

And to get values ​​from two actions without changing activity, 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);
                }
            }
        }

Action 2

add the following code to return to activity 1 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

0
source

All Articles