OnSaveInstanceState not working

when the screen rotates ... Toast doesn't print anything!

public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override protected void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); String a = savedInstanceState.getString("hello"); Toast.makeText(MainActivity.this, a, Toast.LENGTH_SHORT).show(); } @Override public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) { super.onSaveInstanceState(outState, outPersistentState); String a = "WTF"; outState.putString("hello",a); } 

}

I proclaimed everything beautifully, where is the bummer in this simple code!

+5
source share
1 answer

I think you have fallen into a really common trap with which many developers have since overloaded the onSaveInstanceState() method with the Android OS team.

You are overriding the wrong method . Do you want to:

 @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); String a = "WTF"; outState.putString("hello",a); } 

Personally, I think Craig Mautner should be forced to donate money every time an Android developer makes this mistake - source

+11
source

All Articles