-
-
Save nesquena/8b9f9ec29582afd4d138 to your computer and use it in GitHub Desktop.
Android FragmentTabListener for ActionBarActivity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.codepath.example.actionbarcompat; | |
import android.support.v4.app.Fragment; | |
import android.support.v4.app.FragmentActivity; | |
import android.support.v4.app.FragmentTransaction; | |
import android.support.v7.app.ActionBar.Tab; | |
import android.support.v7.app.ActionBar.TabListener; | |
public class SupportFragmentTabListener<T extends Fragment> implements TabListener { | |
private Fragment mFragment; | |
private final FragmentActivity mActivity; | |
private final String mTag; | |
private final Class<T> mClass; | |
private final int mfragmentContainerId; | |
private final Bundle mfragmentArgs; | |
public SupportFragmentTabListener(FragmentActivity activity, String tag, Class<T> clz) { | |
mActivity = activity; | |
mTag = tag; | |
mClass = clz; | |
mfragmentContainerId = android.R.id.content; | |
mfragmentArgs = new Bundle(); | |
} | |
public SupportFragmentTabListener(int fragmentContainerId, FragmentActivity activity, String tag, Class<T> clz) { | |
mActivity = activity; | |
mTag = tag; | |
mClass = clz; | |
mfragmentContainerId = fragmentContainerId; | |
mfragmentArgs = new Bundle(); | |
} | |
// This version supports specifying the container to replace with fragment content and fragment args | |
// new SupportFragmentTabListener<SomeFragment>(R.id.flContent, this, "first", SomeFragment.class, myFragmentArgs)) | |
public SupportFragmentTabListener(int fragmentContainerId, FragmentActivity activity, | |
String tag, Class<T> clz, Bundle args) { | |
mActivity = activity; | |
mTag = tag; | |
mClass = clz; | |
mfragmentContainerId = fragmentContainerId; | |
mfragmentArgs = args; | |
} | |
/* The following are each of the ActionBar.TabListener callbacks */ | |
public void onTabSelected(Tab tab, FragmentTransaction sft) { | |
// Check if the fragment is already initialized | |
if (mFragment == null) { | |
// If not, instantiate and add it to the activity | |
mFragment = Fragment.instantiate(mActivity, mClass.getName(), mfragmentArgs); | |
sft.add(mfragmentContainerId, mFragment, mTag); | |
} else { | |
// If it exists, simply attach it in order to show it | |
sft.attach(mFragment); | |
} | |
} | |
public void onTabUnselected(Tab tab, FragmentTransaction sft) { | |
if (mFragment != null) { | |
// Detach the fragment, because another one is being attached | |
sft.detach(mFragment); | |
} | |
} | |
public void onTabReselected(Tab tab, FragmentTransaction sft) { | |
// User selected the already selected tab. Usually do nothing. | |
} | |
} |
Good work man.
I have an issue with both this example and the one provided by Android (http://developer.android.com/guide/topics/ui/actionbar.html#Tabs). When using with Otto, I started to notice that I was registering multiple fragments for the same tab. It was similar to the phenomenon reported in http://blog.sqisland.com/2014/06/navigationdrawer-creates-fragment-twice.html
The code doesn't handle orientation changes. Since the private variable mFragment will be destroyed on screen rotation but the fragment itself is restored in the Fragment Manager, you end up always re-creating multiple fragments. But If I find the fragment by tag, I'm able to reuse the old one.
This works better for me:
public void onTabSelected(Tab tab, FragmentTransaction sft) {
FragmentManager fragmentManager = mActivity.getSupportFragmentManager();
mFragment = fragmentManager.findFragmentByTag(mTag);
FragmentTransaction sft = fragmentManager.beginTransaction();
// Check if the fragment is already initialized
if (mFragment == null) {
Thoughts?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
HI, I'm having an issue. When I use this code for android versions below 3.0 the tabs works perfectly. But when I try to test in > 3.0 versions the attached and detached not works shows two fragments one after other.
Any suggestion?
Thanks