Android - save member field value in fragment class

I am trying to do something using boolean in the Fragment class every time a Fragment displayed.

Example

My application launches, opens FirstFragment and boolean for the first time always true , then I have an if clause that checks its value:

 if (FirstTime) { FirstTime = false; } else { // Other stuff here, cause it not true. } 

Then, for the first time, FirstTime is true , I do something like go to another Fragment . and when I get back to Fragment1 and on my onCreate() , I will do the same. It is always true , it seems that it is refreshing or something like that.

Then I thought that this might be a problem with Fragment , and every time I click on Fragment1 , it restarts or something like that. Then I added getter and setter to my MainActivity :

 public Boolean getFirstTime() { return FirstTime; } public void setFirstTime(Boolean FirstTime) { this.FirstTime = FirstTime; } 

where from the start, it's true, and then I changed the code from Fragment1 to:

 if (((MainActivity) getActivity()).getFirstTime()) ((MainActivity) getActivity()).setFirstTime(false); } else { // Other stuff here, cause it not true, } 

However, he is still talking about it.

What am I doing wrong or what did I misunderstand about fragments?
Is there any way to do this?

+1
java android android-lifecycle sharedpreferences android-fragments
May 2 '15 at 19:40
source share
2 answers

You have made the assumption that the Fragment instance remains in place as long as the application remains alive. This is a reasonable assumption, and your approach will work fine if that assumption is true.

Unfortunately, a Fragment destroyed when it recedes in the background and is recreated when it returns to the foreground. That is why it seems to be "being updated." The same goes for Activity . When an Activity recedes into the background, it is not destroyed immediately. Rather, it has been supported in the current backstack task for some time, and if it returns to the forefront, it is the same instance.

There are four different ways to deal with this problem:

  • Declare FirstTime as static . That should work. I have used this before. However, this should be used only in extreme cases when there is an absolute need to preserve the value of a member field and only when there is no other way. Creating a static variable results in a classic memory leak.
  • Save the FirstTime value to Fragment with onSaveInstanceState() :

     @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putBoolean("FirstTime", FirstTime); } 

    and get the value in onCreate() :

     @Override public void onCreate (Bundle savedInstanceState){ super.onCreate(); FirstTime = savedInstanceState.getBoolean("FirstTime"); } 
  • Declare FirstTime in a global constant class instead of putting it in a Fragment :

     public class GlobalConstants{ public static boolean FirstTime = true; // other global constants ... } 

    and access it in Fragment as follows:

     if (GlobalConstants.FirstTime) { GlobalConstants.FirstTime = false; } else { //Other stuff here cause it not true } 
  • Save the FirstTime value to SharedPreference :

     SharedPreferences sp = getActivity().getPreferences(Context.MODE_PRIVATE); SharedPreferences.Editor editor = sp.edit(); editor.putBoolean("FirstTime", FirstTime); editor.commit(); 

    and get its value like this:

     SharedPreferences sp = getActivity().getPreferences(Context.MODE_PRIVATE); FirstTime = sp.getBoolean("FirstTime", true); 

The first three methods will maintain the FirstTime value while the application is active. The fourth method will save the FirstTime value for the lifetime of the application, that is, when the application restarts, FirstTime will be true or false depending on which value was set last before the application FirstTime .

Literature:

1. Processing the life cycle of a fragment .

2. Saving sets of key values .

3. Visibility and life expectancy .

EDIT:

To understand how to use onSaveInstanceState() , see the following links:

1. Saving (and getting) the state of the Android instance . p>

2. Once all, how to properly maintain the state of the fragment instance .

3. Processing configuration changes .

This is confusing, but it will be useful to you as soon as you understand it.

+4
May 03, '15 at 8:57
source share

A simple solution, set the boolean value to static.

This, of course, is not consistent with good programming practice.

To answer the question more directly, I assume that the fragments and actions will be destroyed and new instances will be created, so the boolean value will be set to true again. Thus, making the variable static, it will remain in all instances of this class.

+1
May 02 '15 at 20:03
source share



All Articles