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





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















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. I.e. 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.










share|improve this question































    0















    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. I.e. 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.










    share|improve this question



























      0












      0








      0








      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. I.e. 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.










      share|improve this question
















      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. I.e. 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.







      java android






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 3 at 7:31









      DawidJ

      760717




      760717










      asked Jan 3 at 7:14









      TristusTristus

      4319




      4319
























          1 Answer
          1






          active

          oldest

          votes


















          0














          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");





          share|improve this answer
























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54017856%2frestoring-state-not-working-when-returning-to-activity-savedinstancestate-is-nu%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            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");





            share|improve this answer




























              0














              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");





              share|improve this answer


























                0












                0








                0







                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");





                share|improve this answer













                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");






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jan 3 at 7:19









                Tim BiegeleisenTim Biegeleisen

                237k13100160




                237k13100160
































                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54017856%2frestoring-state-not-working-when-returning-to-activity-savedinstancestate-is-nu%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

                    Does disintegrating a polymorphed enemy still kill it after the 2018 errata?

                    A Topological Invariant for $pi_3(U(n))$