2

Fragment losing state and shows an empty UI if left idle for 20 minutes. I'm using FragmentStatePagerAdapter and I have tried calling the notifyDataSetChanged in onStart() of my FragmentActivity.

Kindly help me how to save the object and state of my fragment and reuse it on reload of the app.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437

4 Answers4

3

Android can kill your app if needed, you need to use onSaveInstanceState to keep your state in this cases. (Remember: Save important data in onPause!)

onSaveInstanceState exists in Activity and Fragments and is used in the same way like an activity

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putInt("integer", anInteger);
}

Now in onCreate, onCreateView or onActivityCreated you have this argument BundlesavedInstanceState which corrisponds to the bundle saved. (Check if it's null too.)


If not enought maybe Android killed your FragmentManager too, so you need to override onSaveInstanceState and onRetoreInstanceState in your Activity and restore the fragment.

Maybe this answer could help you about the last thing i said: Using onSaveInstanceState with fragments in backstack?

Community
  • 1
  • 1
Marco Acierno
  • 14,682
  • 8
  • 43
  • 53
  • +1 for an answer with more explanation than mine. You should just add the example of how to check onSaveInstanceState != null – HpTerm Apr 07 '14 at 13:36
0

Hard to answer without your code.

However I can say that the state is usually saved by the savedInstanceState

Usually in the onActivityCreated you have something like the following. In the example I give I save a boolean of either text was entered or not.

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    if (savedInstanceState != null) {
        mHasText = savedInstanceState.getBoolean(HAS_TEXT_TAG);
    } else {
       // do something
    }
}

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

it's just an example as without your code it's difficult to anwer

HpTerm
  • 8,151
  • 12
  • 51
  • 67
0

A Fragment's life-cycle is closely tied to the Activity's lifecycle. This means, when your Activity goes idle; it will kill off any contained Fragments. To store Fragments you could always retain them in concordance with the Fragment API. This means you will generally be using the Fragment in a background. However the best way to keep a from being destroyed or lost from an Activity's end would be to store relevant information in a custom object and then to recreate the Fragment when the Activity is resumed.

For instance; I could have a custom object that would store relevent UI values for my Fragment and when my Activity either idles or changes I would save those relevant values to my custom object that I created. Then, when either a new Activity is created; or my old Activity is resumed; I would retrieve those values and put them back into my Fragment's UI. Hoped this helped :)

Timothy Frisch
  • 2,995
  • 2
  • 30
  • 64
  • As I mentioned in my question I'm already calling the notifydatasetchanged of the view pager adapter in the onStart method of my fragment activity but unfortunately it hasn't solved my problem. I designed the adapter so that it doesn't recreate the fragment object whenever the notifydatasetchanged is called but uses the earlier object. Do you think i should also call the refresh ui method on my fragment?? – user3506878 Apr 07 '14 at 13:55
0

In case android needs memory, it kills the running apps. So you must save the objects using

public void onSaveInstanceState(Bundle savedState) {}

Note that savedState must be serializable.

You must call notifyDataSetChanged() in onResume(), because it ensures that it is called when the activity resumes.

For a detailed answer, please post your code.

user3388324
  • 572
  • 5
  • 18