Skip to content

Instantly share code, notes, and snippets.

@mpaloulack
Last active August 29, 2015 13:59
Show Gist options
  • Save mpaloulack/10472674 to your computer and use it in GitHub Desktop.
Save mpaloulack/10472674 to your computer and use it in GitHub Desktop.
Utilisation des fragment
Fragment frag;
//ajout d'un fragment
TonFragment newFragment = new TonFragment();
android.support.v4.app.FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.add(R.id.fragment_container, newFragment).addToBackStack("A_B_TAG");
transaction.commit();
//remplacement d'un fragment
frag = new AutreFragment();
android.support.v4.app.FragmentManager fm = getSupportFragmentManager();
android.support.v4.app.FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, frag);
fragmentTransaction.setTransition(fragmentTransaction.TRANSIT_FRAGMENT_OPEN);
fragmentTransaction.commit();
// Utuiliser le back button
@Override
public void onBackPressed() {
if (getSupportFragmentManager().findFragmentByTag("FragmentC") != null) {
getSupportFragmentManager().popBackStack("A_B_TAG",
fm.POP_BACK_STACK_INCLUSIVE);
} else {
super.onBackPressed();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment