-
-
Save thoretton-edwin/10477582 to your computer and use it in GitHub Desktop.
Android remplacer un fragment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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