user3506878
user3506878

Reputation: 29

Fragment state lost when device left idle

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.

Upvotes: 2

Views: 1806

Answers (4)

HpTerm
HpTerm

Reputation: 8281

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

Upvotes: 0

Marco Acierno
Marco Acierno

Reputation: 14847

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?

Upvotes: 3

user3388324
user3388324

Reputation: 572

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.

Upvotes: 0

Timothy Frisch
Timothy Frisch

Reputation: 2809

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 :)

Upvotes: 0

Related Questions