user2582401
user2582401

Reputation: 81

Fragment transaction on a single tab of ViewPager in SherlockFragmentActivity

I am creating ViewPager in SherlockFragmentActivity. Structure of my app is like ViewPager->having three tabs: tab1, tab2, tab3. In tab2, I want to display different fragments like fragment1->fragment2->fragment3. In ViewPager, I am having three tabs (Fragments) added using FragmentStatePageAdapter as shown below.

public Fragment getItem(int arg0) {
    switch (arg0) {
        case 0:
            SearchTab searchtab = new Tab1();
            return tab1;
        case 1:
            BrowseTab browsetab = new Tab2();
            return tab2;
        case 2:
            SavedItemsTab savedTab = new Tab3();
            return Tab3;
    }
    return null;
}

Now in tab2, I have displayed ListView in fragment1. When user clicks an item from ListView, I want to display another fragment (fragment2) with other ListView. I am doing this by fragment transaction as bellow:

Fragment newFragment = new Brxxxx_Fragment();

Bundle args = new Bundle();
args.putString("Main_Cat_ID", Main_Category_ID_Str);
args.putString("Main_Cat_Name", Main_Category_Name_Str);

newFragment.setArguments(args);

FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.b_p_layoutid, newFragment);
transaction.addToBackStack(null);
transaction.commit();

After this step, I am able to display another fragment (fragment2), but my old fragment (fragment1) is still visible below the new fragment.

If I want to remove previous fragment without using replace(), how should I get the current fragment to pass in remove method?

Upvotes: 1

Views: 260

Answers (1)

djardon
djardon

Reputation: 161

Maybe you can remove the other fragment if you don't need to navigate back or set background color to the new fragment . You can check that your layout id 'R.id.b_p_layoutid' is of your 'FrameLayout' and not your 'Fragment' id just in case...

android:background="?android:attr/colorBackground"

getActivity().getSupportFragmentManager().beginTransaction().remove('YOUR_FRAGMENT').commit();

You can check this answer too:

Visit Android replace fragment still displays some of the replaced fragment

Visit Fragment replaced still visible on background

Upvotes: 1

Related Questions