Home > OS >  how can i hide fragment via a button located inside the same fragment?
how can i hide fragment via a button located inside the same fragment?

Time:11-07

i'm calling fragment via button within the main activity with this (i'm using bundle to send data here)




String ScoreText ; 
Scooore myFragment = new Scooore();

FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
Bundle data = new Bundle();
data.putString("score", scoreText);
myFragment.setArguments(data);
fragmentTransaction.replace(R.id.showScore, myFragment).commit();



then inside my score fragment i have button there , and i wanna know what code shall i put inside the onClicklistener method there so that button wpild take me back to the main activity

i can use new intent and go back there or even hide the frame layout using setVisibility on it , but ig maybe there's better options, right ?

CodePudding user response:

In your MainActivity, add transaction to back stack:


fragmentTransaction.beginTransaction().addToBackStack("scoore");

and then in your fragment pop it from the back stack:

getActivity().getSupportFragmentManager().popBackStack();

  • Related