Hey Guys!

So I have been trying for a while but I cannot, for the life of me, 
understand how to retain a Fragment's state after navigating away from it 
(to a different tab on an ActionBar for example). 

So basically, I have an Activity, which has nothing on it, but an ActionBar 
with Navigation_Mode_Tabs implemented, the ActionBar has two tabs, and each 
tab calls on a different ListFragment. Each ListFragment has to parse 
images from URLs which I am already sizing decently to a 48*48 sample size 
to avoid memory overhead, ideally I should be able to switch from Fragment 
A to Fragment B and not have my scroll position lost, neither should the 
Fragments attempt to re-download the images on the listviews; however, 
every time I change from a tab to the other, the whole View is recreated. 

I read somewhere that adding "*this.setRetainState(true)"* on the 
constructor could help, Tried that, and it still recreates the entire view 
after changing tabs. I also tried calling *addBackToStack() *on the actual 
Activity that works as the container but that did not work well (see Stack 
Overflow<http://stackoverflow.com/questions/12723592/calling-addtobackstack-on-a-fragment-that-is-added-via-actionbar>).
 
I have also tried tinkering with *onTabSelected(),* and *onTabUnselected()* 
methods, 
like changing from *add(mFragment) *to *attach(mFragment)* and from *
detach()* to *replace(), *did not work. I then read that checking for a *
savedInstanceState* on the *onCreateView()* method could save us from 
having to recreate, but I guess that would imply that I'd have to actually 
save something during *onPause() *or during *onSaveInstanceState()* and 
that would still mean I have to recreate the view, except that I'd be 
loading from a local cache rather than redownloading, but that still uses 
overhead and defeats the purpose. 

As you can see below, I finally gave up and just checked for *if 
(savedInstanceState != null)* and if it was not null, then I'd let it 
return, but that does not do anything and in fact stops the view from being 
created at all. 

Here is my code, which is a pretty simple Fragment, please let me know if 
you have any better ideas I could try :)

public class TimelineFragment extends ListFragment{
 private TimelineAdapter adapter;
private ArrayList<String> names = new ArrayList<String>();
private ArrayList<String> statuses = new ArrayList<String>();
private ArrayList<String> urls = new ArrayList<String>();
 public TimelineFragment () {
this.setRetainInstance(true);
}
 @Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
 if (savedInstanceState != null) {
return;
}
names.add("Daniel Alvarado");
names.add("Melida Salinas");
names.add("Vanny Maltez");
 statuses.add("Jamas habia visto yo una cuenta de *debito* en negativo...");
statuses.add("Que cagada... y yo que iba a ir al concierto :/");
statuses.add("Yeah, that was not even a full 30 minutes before I ran an 
installed Google Chrome. Nice try IE10.");
 
urls.add("https://fbcdn-profile-a.akamaihd.net/hprofile-ak-snc7/372694_824208210_1645128195_q.jpg";);
urls.add("https://fbcdn-profile-a.akamaihd.net/hprofile-ak-snc6/187193_628399807_642623273_q.jpg";);
urls.add("https://fbcdn-profile-a.akamaihd.net/hprofile-ak-prn1/48738_610157012_1827552619_q.jpg";);
 adapter = new TimelineAdapter(getActivity(), statuses, names, urls);
 setListAdapter(adapter);
}
}

Here is the TabListener I implemented on the Activity that contains the 
Fragment:

public static class TabListener<T extends Fragment> implements 
ActionBar.TabListener {
        private Fragment mFragment;
        private final Activity mActivity;
        private final String mTag;
        private final Class<T> mClass;

        public TabListener(Activity activity, String tag, Class<T> clz) {
            mActivity = activity;
            mTag = tag;
            mClass = clz;
        }

        public void onTabSelected(Tab tab, FragmentTransaction ft) {
            if (mFragment == null) {
                mFragment = Fragment.instantiate(mActivity, 
mClass.getName());
                ft.add(android.R.id.content, mFragment, mTag);
            } else {
                ft.attach(mFragment);
            }
        }

        public void onTabUnselected(Tab tab, FragmentTransaction ft) {
            if (mFragment != null) {
                ft.detach(mFragment);
            }
        }

        public void onTabReselected(Tab tab, FragmentTransaction ft) {
        }
    }

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to