Android - Save Value Of Member Field In Fragment Class
Solution 1:
You have made an assumption that the Fragment
instance remains in existence as long as the app is alive. It is a reasonable assumption, and your approach would work fine if that assumption were true.
Unfortunately, a Fragment
is destroyed when it recedes into the background, and created anew when it returns to the foreground. This is why it appears to "refresh". The same is not true of an Activity
. When an Activity
recedes into the background, it is not destroyed immediately. Rather, it is maintained on the current task's backstack for some time, and if it returns to the foreground, it is the same instance.
To combat this problem, there are four different ways:
- Declare
FirstTime
asstatic
. This should work. I've used this before. However, this should only be used in extreme cases when there is an absolute necessity to preserve the value of a member field, and only when no other way is available. Making a variablestatic
leads to a classic memory leak. Save the value of
FirstTime
in yourFragment
usingonSaveInstanceState()
:@OverridepublicvoidonSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putBoolean("FirstTime", FirstTime); }
and retrieve the value in
onCreate()
:@OverridepublicvoidonCreate(Bundle savedInstanceState){ super.onCreate(); FirstTime = savedInstanceState.getBoolean("FirstTime"); }
Declare
FirstTime
in a global constants class instead of putting it in theFragment
:publicclassGlobalConstants{ publicstaticbooleanFirstTime=true; // other global constants ... }
and access it in your
Fragment
like this:if (GlobalConstants.FirstTime) { GlobalConstants.FirstTime = false; } else { //Other stuff here cause it's not true }
Save the value of
FirstTime
in aSharedPreference
:SharedPreferencessp= getActivity().getPreferences(Context.MODE_PRIVATE); SharedPreferences.Editoreditor= sp.edit(); editor.putBoolean("FirstTime", FirstTime); editor.commit();
and retrieve its value in this way:
SharedPreferencessp= getActivity().getPreferences(Context.MODE_PRIVATE); FirstTime = sp.getBoolean("FirstTime", true);
The first three methods will maintain the value of FirstTime
while the application is alive. The fourth method will preserve the value of FirstTime
beyond the lifetime of the application, i.e. when the app restarts, FirstTime
will be true
or false
depending on what its value was last set before the app exited.
References:
1.Handling the Fragment Lifecycle.
EDIT:
To understand how to use onSaveInstanceState()
, see the following links:
1.Saving (and Retrieving) Android Instance State.
2.Once for all, how to correctly save instance state of Fragments.
3.Handling Configuration Changes.
It is confusing, but it will be useful to you once you understand it.
Solution 2:
Simple solution, set your boolean to a static.
This of course does not comply to the good practices of programming.
To answer you question more directly, I'm assuming that the fragments and activities get destroyed and new instances are created, hence the boolean being set to true again. As such, by making the variable static, it's state will remain across all instances of that class.
Post a Comment for "Android - Save Value Of Member Field In Fragment Class"