简体   繁体   中英

onRestoreInstance not getting called while switching between activities

I m trying to switch between two activities . In activity 1, i have Button named GoToSecond to goto second activity. In the same way,in activity 2, have Button named GoToFirst to goto first activity.

I have used log messages in first activity .

Order I m getting when GoToSecond button is clicked is onCreate

onStart

onResume

onSaveInstance

onStop

and it switches to second activity . Now in second activity when i click button GoToFirst , first activity opens and log order in first activity is

onCreate

onStart

onResume ..

why onRestoreInstance is not getting called after onStart? even if instance is stored??

Can anyne help me?

Here is the code of first activity:

public class MainActivity extends AppCompatActivity {
EditText hello;
Button b1;
public static String TAG="Prajwal";


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    hello=(EditText) findViewById(R.id.hello);
    Log.d(TAG,"OnCreate counter 1");
    b1=(Button) findViewById(R.id.b1);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
    Intent i=new Intent(MainActivity.this,Main2Activity.class);
    startActivity(i);
}
});

I havent added log messages in code .

And second activity code is

public class Main2Activity extends AppCompatActivity {
Button b2;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
    b2=(Button) findViewById(R.id.b2);
    b2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           Intent i=new Intent(Main2Activity.this,MainActivity.class);
              startActivity(i);
        }
    });
    Log.d(MainActivity.TAG,"Oncreate 2");

}

For example If i hv an EditText in first activity , and if i hav some text in it , I m losing that text while switching between 2nd activity to 1st activity . bcoz onCreate is called in first activity. But when i switch to landscape mode onRestoreInstance is called and im not losing any text .

As I know, onRestoreInstance will get called only if your activity will be destroyed and created again. So in this case your will restore previous state of destroyed activity.

But in your case activity isn't destroyed, it just stopped by the system, so it is not loose its state, so no need to restore it.

Please see these links for more details: FIRST , SECOND

Edited

Since you provided code of your Activities, as I see, you open from second activity not first activity, but third activity with type of first Activity. So your stacktrace after pressing on button in second Activity would be:

MainActivity -> Main2Activity -> MainActivity.

So you have two simple solutions: 1) Instead of

Intent i=new Intent(Main2Activity.this,MainActivity.class);
startActivity(i);

Just write finish();

2) Add flag FLAG_ACTIVITY_CLEAR_TOP to intent from Main2Activity to MainActivity

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