21

I want to ignore or disable savedInstanceState so that the state of current activity won't save when I go to the next activity.

public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
TylerH
  • 20,799
  • 66
  • 75
  • 101
Kris
  • 3,709
  • 15
  • 50
  • 66

4 Answers4

21

Just this:

public void onCreate(Bundle savedInstanceState){
    super.onCreate(null);
Vladimir Ivanov
  • 42,730
  • 18
  • 77
  • 103
  • Thanks for your response, but why [it doesn't work here](http://stackoverflow.com/questions/6197594/android-how-not-to-save-instance-state/)? It seemed like the instance is still saved. – Kris Jun 01 '11 at 08:53
  • Actually, the instance is saved, but you don't use it. – Vladimir Ivanov Jun 01 '11 at 09:01
13

Write this line in your activity xml file:

    android:saveEnabled="false"
Alex Timpau
  • 153
  • 1
  • 9
  • 1
    This will not save instance state for the activity, but its children may still be saving and restoring state, which was the case for me. I had to put `android:saveEnabled="false"` on the individual xml elements that I didn't want to save state. – Mike Hall Dec 01 '21 at 16:34
9

Or this:

@Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.clear();
    }
R00We
  • 1,931
  • 18
  • 21
0

Just override onRestoreInstanceState without calling the super version

@Override
    protected void onRestoreInstanceState(Bundle outState) {
        //Just leave it empty
    }
Mohamed Medhat
  • 761
  • 11
  • 16
  • But then you're not addressing the potential `TransactionTooLargeException` that occurs when saving instance state: https://issuetracker.google.com/issues/37103380 – Gavin Wright Jan 08 '22 at 03:26
  • @GavinWright Thanks for sharing this. I didn't know that saving state may produce such issue. Btw, instead of my provided answer, is it possible to override `onSaveInstanceState` without calling `super.onSaveInstanceState(outState)` as an answer to this question? – Mohamed Medhat Jan 29 '22 at 11:48