简体   繁体   中英

Restoring state not working when returning to activity, savedInstanceState is null

So im having some trouble restoring the state of my Activity . At this point I figure that its probably a problem with my understanding rather then anything else.

My goal is to move from my MainActivity to a Main2Activity . Am I correct in thinking that when a user moves from one page to another, it should be done by changing Activity via Intent ? I am doing this like so:

The onCreate() for my MainActivity has this in it.

Button currentButton = (Button) findViewById(R.id.button2);
currentButton.setOnClickListener(
    new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            setContentView(R.layout.activity_main2);
            Intent nextIntent = new Intent(getApplicationContext(), Main2Activity.class);
            startActivity(nextIntent);
        }
    }
);

Which as I understand should call onCreate() , onStart() and onResume() for Main2Activity, then onSaveInstanceState() for MainActivity , then onStop() for MainActivity . Ive overloaded all those functions with logging and seen that indeed they are being called and in that order.

Here is my onSaveInstanceState() for MainActivity :

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    System.out.println("onSaveInstanceState called, saving state");
    savedInstanceState.putInt("mySuperUniqueKey", testInt);

    super.onSaveInstanceState(savedInstanceState);
}

Once in Main2Activity , I return back to MainActivity in the same way. Ie findViewById() the button, overload its onClickListener() , create a new Intent and start it.

Then MainActivity class's onCreate() has this :

if (savedInstanceState != null) {
    System.out.println("savedInstanceState is not null");
    testInt = savedInstanceState.getInt("mySuperUniqueKey");
} else {
    System.out.println("savedInstanceState is null");
}

When returning back the MainActivity from Main2Activity , I can see from the logging that onCreate() , then onStart() , then onResume() for MainActivity is called, then onStop() for Main2Activity . Unfortunatly the logging shows that savedInstanceState always comes back as null.

To add to this, when in the emulator, switching the orientation back and forth causes this to work perfectly; savedInstanceState is not null and features the saved testInt. Thus I figure its a problem with my understanding and that there must be something im missing.

My gradle has minSdkVersion set to 16, and targetSdkVersion set to 28. Am I maybe targeting too low a minSdkVersion?

I have read through the "Understand the Activity Lifecycle" on the official android developer documentation but still cant get it. https://developer.android.com/guide/components/activities/activity-lifecycle

I did find similar problems but none of them match my situation exaclty, also the solutions they have suggested I am already doing anyway.

Any insight would be greatly appreciated, thanks in advance.

The saved instance state bundle is intended to save the state of the current activity across things like orientation changes. It is not designed to persist across activities. You should use Intent#putExtra :

nextIntent.putExtra("mySuperUniqueKey", testInt);

Then, in your next activity, access this passed value using:

int testInt = getIntent().getIntExtra("mySuperUniqueKey");

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