Skip to content

Instantly share code, notes, and snippets.

@jillesRagonha
Created February 28, 2018 18:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jillesRagonha/4c184165b92fdf026ab3cd033b64b1bf to your computer and use it in GitHub Desktop.
Save jillesRagonha/4c184165b92fdf026ab3cd033b64b1bf to your computer and use it in GitHub Desktop.
A gist to show how to handle with differents fragments in same activity
public class ReceitasActivity extends AppCompatActivity {
private static final String INGREDIENTE_TAG = "ingrediente_fragment";
private static final String PASSOS_TAG = "passos_fragment";
private static final String VIDEOS_TAG = "videos_fragment";
//Control what fragment is show
private int fragmentoExibido;
private static final int RECEITA_INGREDIENTES = 1;
private static final int RECEITA_PASSOS = 2;
private static final int RECEITA_VIDEOS = 3;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_receitas);
FragmentManager supportFragmentManager = getSupportFragmentManager();
FragmentTransaction tx = supportFragmentManager.beginTransaction();
//Even if my device is on Landscape or Portrait mode, my first container always will be my list of recipes
tx.replace(R.id.frame_principal, listaReceitasFragment);
if(savedInstanceState != null){
//retrieve the fragment saved in savedInstanceState
ingredientesFragment = (IngredientesFragment) getSupportFragmentManager().getFragment(savedInstanceState, INGREDIENTE_TAG);
passosFragment = (PassosFragment) getSupportFragmentManager().getFragment(savedInstanceState, PASSOS_TAG);
playerFragment = (PlayerFragment) getSupportFragmentManager().getFragment(savedInstanceState, VIDEOS_TAG);
fragmentoExibido = savedInstanceState.getInt("fragmentoExibido"); // Retrieve the last fragment that was showed
//and the magic is here
if (estaNoModoPaisagem()) {
switch (fragmentoExibido) {
case RECEITA_INGREDIENTES:
supportFragmentManager.popBackStackImmediate(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
supportFragmentManager.executePendingTransactions();
supportFragmentManager.beginTransaction().remove(ingredientesFragment).commit();
tx.replace(R.id.frame_secundario, ingredientesFragment);
break;
case RECEITA_PASSOS:
supportFragmentManager.popBackStackImmediate(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
supportFragmentManager.executePendingTransactions();
supportFragmentManager.beginTransaction().remove(passosFragment).commit();
tx.replace(R.id.frame_secundario, passosFragment);
break;
case RECEITA_VIDEOS:
supportFragmentManager.popBackStackImmediate(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
supportFragmentManager.executePendingTransactions();
supportFragmentManager.beginTransaction().remove(playerFragment).commit();
tx.replace(R.id.frame_secundario, playerFragment);
break;
default:
tx.replace(R.id.frame_secundario, new IngredientesFragment());
}
}
}
}
tx.commit();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment