Reputation: 9507
I want to use multiple fragments inside single tab. my tab activity extends SherlockFragmentActivity
.
Now i add first fragment for first tab in onTabChanged()
method. and i replace another fragment from within first fragment in first tab. But no when i go to second tab and then again re-open first tab, state of first tab not maintained and first fragment become visible rather than second fragment of first tab.
I need help for how to maintain stake of fragments within single tab and how to pop up exiting fragment front within single tab.
Any body have idea ?
Upvotes: 3
Views: 2841
Reputation: 109
Here is a sample code for managing multiple fragments in an activity:
public class MainActivity extends FragmentActivity {
private FragmentManager fragmentManager;
Fragment currentFragment;
Fragment1 initialFragment;
Fragment2 nextFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
fragmentManager = getSupportFragmentManager();
initialFragment = (Fragment1) fragmentManager.findFragmentById(R.id.initial_fragment);
nextFragment = (Fragment2) fragmentManager.findFragmentById(R.id.next_fragment);
nextFragment();
}
public void onNext(View v) {
// User pressed next button on initial fragment
showNextFragment();
}
private void showFragment(Fragment fragmentToShow) {
Fragment[] fragments = { initialFragment, nextFragment /*... */ };
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
for(Fragment fragment : fragments) {
if(fragment != fragmentToShow) {
fragmentTransaction.hide(fragment);
}
}
fragmentTransaction.show(fragmentToShow);
fragmentTransaction.commit();
}
private void showInitialFragment() {
currentFragment = initialFragment;
showFragment(currentFragment);
}
private void showNextFragment() {
currentFragment = nextFragment;
showFragment(currentFragment);
}
}
Upvotes: 0
Reputation: 1534
If you are using two tabs, you have to maintain two fragments separately for each tab i.e FragmentA for tab1 and FragmentB for tab2 and xmls of FragmentA , FragmentB should have FrameLayout for FrgmentTransaction.
xml of FragmentA i.e frag_a.xml :
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragmentA_container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
and
xml of FragmentB i.e frag_b.xml :
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragmentB_container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
For Tab1, always do the transaction using R.id.fragmentA_container. i.e replace another fragment from within first fragment(FragmentA) in first tab. like below code
In FragmentA:
Fragment fragment = new YourFragment();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
getSupportFragmentManager().popBackStack();
fragment.setArguments(null);
ft.replace(R.id.fragmentA_container, fragment);
ft.commit();
For Tab2, always do the transaction using R.id.fragmentB_container. i.e replace another fragment from within secondfragment(FragmentB) in second tab. like below code
In FragmentB:
Fragment fragment = new YourFragment();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
getSupportFragmentManager().popBackStack();
fragment.setArguments(null);
ft.replace(R.id.fragmentB_container, fragment);
ft.commit();
Upvotes: 1