java - Android savedInstanceState return always null -
I have Fragment A and Fragmentab, I am in a freaktab when I go back to Fragment A, give me the last value Need to load, but my saved instenstate always returns empty:
Expands the public stable square placeholder fragment {Private Boolean active = false; Override public null at @SaveInstanceState (bundle outstate) {super.onSaveInstanceState (outstate); OutState.putBoolean ("active", active); } @ Override Crate on Public Zero (Bundle Saved Instantstate) {Super.Nought (Saved Instantstate); } @ Override Public Empty on Activity Crateed (Bundle Saved Instantstate) {Super. On-activity creativity (saved instenstate); If (saved instenestate! = Blank) {active = savedInstanceState.getBoolean ("active"); If (ativa == true) {toast. Make text (getActivity), "True", toast. LNNGHHORT). Show (); } And {toast} MakeActivity (), "false", toast .LENGTH_SHORT). Show (); }}}
Use: I) addToBackStack ("Back") (committed).
Bundle saved Instantstate will always be nullified in this particular situation because it is only if your piece Was destroyed by the Android system and rebuilt (such as when the screen is rotated or your system is destroyed due to low system resources). The saved InstantState is not used to constantly store data in an app, if you want to do this, then I suggest using the shared suggestions:
Private zeros save booleanopurefons (string key, boolean boole) {SharedPreferences preferences = PreferenceManager .getDefaultSharedPreferences (this); SharedPreferences.Editor Editor = preferences.edit (); Editor. Input boolean (key, bull); Editor.apply (); } Private Boolean getBooleanFromPreferences (string key) {SharedPreferences Preferences = PreferenceManager.getDefaultSharedPreferences (this); Boolean bool = preferences.getBoolean (key, wrong); Return bool; }
Therefore, your code will be something like this:
Public void (outlet bundle) on the Savest Instestestate {super.onSaveInstanceState (outstate); OutState.putBoolean ("active", active); SaveBooleanToPreferences ("active", active); } @ Override Crate on Public Zero (Bundle Saved Instantstate) {Super.Nought (Saved Instantstate); } @ Override Public Empty on Activity Crateed (Bundle Saved Instantstate) {Super. On-activity creativity (saved instenstate); If (Saved Instance State! = Blank) {active = getBooleanFromPreferences ("Active") if (attivo == true) {Toast. MakeActivity (), "True", Toast.lNGTH_SHORT). Show (); } And {toast} MakeActivity (), "false", toast .LENGTH_SHORT). Show (); }}
Comments
Post a Comment