Skip to content

Instantly share code, notes, and snippets.

@nesquena
Last active May 10, 2016 04:31
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nesquena/8b9f9ec29582afd4d138 to your computer and use it in GitHub Desktop.
Save nesquena/8b9f9ec29582afd4d138 to your computer and use it in GitHub Desktop.
Android FragmentTabListener for ActionBarActivity
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.
}
}
@omgg
Copy link

omgg commented Feb 22, 2014

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

@sadegh
Copy link

sadegh commented Jul 21, 2014

Good work man.

@rogerhu
Copy link

rogerhu commented Dec 31, 2014

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