TommyLeeJones TommyLeeJones - 11 months ago 44
Android Question

Static field in a Fragment

I have a FragmentActivity from which I call a Fragment within which I have a static field and ExpandableListView.

After pressing exist or back button I re -open the app and the static field's value is still there (ExpandableListView is in the same shape as well). Which is what I expected. Unfortunately, after a while (but not always) when I go back to the app the static field has its default value rather than the latest one which I presume would mean that the Fragment class was instantiated again. But ExpandableListView is as it was before leaving the app originally.

Why does such behavior happen?

By the way, I am doing this because I want to remember which which group of items is expanded in ExpandableListView.

Here is the pager adapter that calls a Fragment.

public class CollectionPagerAdapter extends FragmentStatePagerAdapter {

private Local local;
private Online online;

public CollectionPagerAdapter(FragmentManager fm) {

public Fragment getItem(int i) {
switch (i) {
case 0:
local = new Local();
return local;
case 1:
online = new Online();
return online;
return null;

public int getCount() {
return 2;

public void updateLocal(String update) {


Answer Source

My guess would be that FragmentStatePagerAdapter is keeping the saved information in memory for a period of time before removing them thus you will get the default value when returning after longer period as the data has been reinitialized. If you want to store the data you can use shared preference (for smaller data).

Shared Preference -

SQL database -

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download