0

I have an activity and when I am pressing home button I have the next activity lifecycle:

   D/States: Login: onPause()
   D/States: Login: onStop()
   D/States: onSaveInstanceState
   D/States: Login: onDestroy()

When I again entering my application, I have:

D/States: Login: onStart()
D/States: Login: onResume()
D/States: Login: onPause()
D/States: Login: onResume()

But onRestoreInstanceState() method is not called and activity state is not saved.

Overrides:

@Override
    protected void onStart() {
        super.onStart();
        Log.d(TAG, "Login: onStart()");
    }

    @Override

    protected void onResume() {
        super.onResume();
        Log.d(TAG, "Login: onResume()");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d(TAG, "Login: onPause()");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.d(TAG, "Login: onStop()");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "Login: onDestroy()");
    }
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putString("login", mEmailView.getText().toString());
        outState.putString("password", mPasswordView.getText().toString());
        Log.d(TAG, "onSaveInstanceState");
//        super.onSaveInstanceState(outState);
    }
    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        mEmailView.setText(savedInstanceState.getString("login"));
        mPasswordView.setText(savedInstanceState.getString("password"));
        Log.d(TAG, "onRestoreInstanceState");

    }

As I know, onRestoreInstanceState() method should be called in onResume() or onStart().

I read about configChanges somewhere in Google while searching answer on my question. I have not android:configChanges="keyboardHidden|orientation" or some else android:configChanges in my manifest.

But why it is not called in my activity? Solved.

danyapd
  • 2,516
  • 1
  • 14
  • 23
  • You might have added configChanges to your AndroidManifest. Please paste the manifest snippet here – Abhishek Nandi Nov 25 '19 at 15:44
  • I read about this somewhere in Google while searching answer on my question. I have not `android:configChanges="keyboardHidden|orientation"` or some else `android:configChanges` in my manifest. – danyapd Nov 25 '19 at 15:50
  • why did you comment out super.onSaveINstanceState(outState)? Looks like you save no state since call to super occurs before string storage – Maksim Turaev Nov 25 '19 at 15:56
  • The bundle that was saved in onSaveInstanceState is available in onCreate – Abhishek Nandi Nov 25 '19 at 15:56
  • Check https://stackoverflow.com/questions/4096169/onsaveinstancestate-and-onrestoreinstancestate – Abhishek Nandi Nov 25 '19 at 15:57
  • Does this answer your question? [onSaveInstanceState () and onRestoreInstanceState ()](https://stackoverflow.com/questions/4096169/onsaveinstancestate-and-onrestoreinstancestate) – Abhishek Nandi Nov 25 '19 at 15:58
  • @MaksimTuraev I have tried both variants of calling `super.` in this method – danyapd Nov 25 '19 at 15:59
  • @AbhishekNandi you are talking about restoring state in `onCreate()` method? Like `if (savedInstanceState != null) { mEmailView.setText(savedInstanceState.getString("login")); mPasswordView.setText(savedInstanceState.getString("password")); }`? If yes - it is not working. – danyapd Nov 25 '19 at 16:01
  • Generally you would use `onCreate(savedInstanceState)` in place of `onRestoreInstanceState` for sake of reliability. – EpicPandaForce Nov 25 '19 at 16:20
  • 1
    And there is an anomaly in log D/States: Login: onStart() D/States: Login: onResume() D/States: Login: onPause() D/States: Login: onResume(), some strange onPause onResume calls. And you should also log oncreate. Github sample would be even better – Maksim Turaev Nov 25 '19 at 16:53
  • 1
    It is not normal that your `Login` `Activity` is being destroyed when the user presses the HOME button. Please post your manifest, and the code that starts `Login` (if this isn't the root `Activity`) as that may help to explain why it is being destroyed. – David Wasser Nov 25 '19 at 17:10

3 Answers3

0

From the official documentations:

This method is called after onStart() when the activity is being re-initialized from a previously saved state, given here in savedInstanceState. Most implementations will simply use onCreate(Bundle) to restore their state, but it is sometimes convenient to do it here after all of the initialization has been done or to allow subclasses to decide whether to use your default implementation. The default implementation of this method performs a restore of any view state that had previously been frozen by onSaveInstanceState(Bundle)

Activity is re-initialized either after orientation change or being recreated after being killed by OS due to memory limitation.

In your case by pressing home button the activity just goes to to stopped state, unless your phone is running low on resources like memory, then get killed by OS, in that case onRestoreInstanceState() will be called

Mohammad Sianaki
  • 1,425
  • 12
  • 20
  • sorry, but I don't understand how this should help to call minimized activity in state, which was saved in `onSaveInstanceState` method – danyapd Nov 25 '19 at 16:04
0

When you press the home button, your activity is not destroyed or recreated. When you bring it back from recent apps, the activity starts and will not be recreated unless it was killed by OS to reclaim memory if the memory was low. It is behaving as expected

Abhishek Nandi
  • 4,265
  • 1
  • 30
  • 43
0

It's not a duplicate...

Ok.

Earlier I was in need to destroy some other activity by any way when the app is minimized. So I used android:noHistory.

And of course I have created "Login" activity with copy-paste and forgot to delete noHistory.

danyapd
  • 2,516
  • 1
  • 14
  • 23