Rohodude
Rohodude

Reputation: 63

Same Fragment appears in all tabs

I have encountered a problem in my Android application. When I added two tabs, I also had two fragments. But the thing is that the same fragment is appearing on both tabs. The other fragment is not showing up. Here is what it looks like.

http://i60.tinypic.com/eu0kzs.png

http://i60.tinypic.com/2uh0i1k.png

Here is my code for my activity:

public class DatabaseFiller extends Activity{
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();

    StrictMode.setThreadPolicy(policy);


    setContentView(R.layout.rmactivity);

    ActionBar actionBar = getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    actionBar.setSubtitle("Created by Rohit Nandakumar");

    /*ActionBar.Tab searchdb = actionBar.newTab().setText("Search Database");
    ActionBar.Tab searchlist = actionBar.newTab().setText("Show List"); */

    ActionBar.Tab Frag1 = actionBar.newTab().setText("Search Database");
    ActionBar.Tab Frag2 = actionBar.newTab().setText("Show List");

    Fragment fragment1 = new DatabaseFillerFragment();
    Fragment fragment2 = new DeleteDBItems();

    Frag1.setTabListener(new MyTabListener(fragment1));
    Frag2.setTabListener(new MyTabListener(fragment2));

    actionBar.addTab(Frag1);
    actionBar.addTab(Frag2);

}
}

Here is my whole MyTabListener class:

package com.example.foodsaver2;

import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener; 
import android.app.ActionBar;
import android.app.Fragment;
import android.app.FragmentTransaction;

public class MyTabListener implements TabListener {

public Fragment fragment;

public MyTabListener(Fragment fragment) {
    this.fragment = fragment;
}

@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
    // TODO Auto-generated method stub
}

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
    // TODO Auto-generated method stub
    if (fragment == null) {
        // If not, instantiate and add it to the activity
        ft.add(android.R.id.content, fragment);
    } else {
        // If it exists, simply attach it in order to show it
        ft.attach(fragment);
    }
}

@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
    // TODO Auto-generated method stub
    if (fragment != null) {
        // Detach the fragment, because another one is being attached
        ft.detach(fragment);
    }
}

}

What am I doing wrong here? Any help regarding this problem would be appreciated.

Upvotes: 3

Views: 344

Answers (2)

sjain
sjain

Reputation: 23344

Initialize the listener, detach the fragment and commit it.

On tab selected just add the fragment else detach.

See the following revised code for help -

public MyTabListener(Activity activity, String tag, Class<T> cls, Bundle args) {
                mActivity = activity;
                mTag = tag;
                mClass = cls;
                mArgs = args;

            fragment  = mActivity.getFragmentManager().findFragmentByTag(mTag);
            if (fragment  != null) {
                FragmentTransaction ft = mActivity.getFragmentManager().beginTransaction();
                ft.detach(fragment);
                ft.commit();
            }
    }


    public void onTabSelected(Tab tab, FragmentTransaction ft) {
                fragment  = Fragment.instantiate(mActivity, mClass.getName(), mArgs);
                ft.add(android.R.id.content, fragment , mTag);
        }

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

Upvotes: 1

Ben
Ben

Reputation: 1295

You need to call the commit method on the FragmentTransaction to trigger the transaction to occur.

Upvotes: 0

Related Questions