• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Proper way to close a fragment

stealthrt

Lurker
Nov 9, 2011
9
1
Hey all I have 3 fragments - activity_main, _fragmoviesand _fragvideoplay.

In the activity_main I have the following:



In my _fragmovies I have the following:



In my _fragvideoplay I have the following:



On the activity_main I have the videoFrame that loads the _fragmovies fragment inside it when it's called by the _fragmovies.

public void startVideo(String mp4Name) {
tabLayout = (TabLayout) findViewById(R.id.mTabLayout);
tabLayout.setVisibility(View.GONE);

myfragment = new videoPlay();

//pass data
Bundle bundle = new Bundle();
bundle.putString("KEY", mp4Name);
myfragment.setArguments(bundle);

fragmentManager = getSupportFragmentManager().beginTransaction();
fragmentManager.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);

frameLayout = findViewById(R.id.videoFrame);
frameLayout.setVisibility(View.VISIBLE);

fragmentManager.replace(R.id.videoFrame, myfragment).commit();
}
The _fragmovies has a temporary button that, when pushed, loads up the _fragvideoplay fragment into the main_activity (the function above) along with passing the name of the video for _fragvideoplay to load up.

MainActivity ma = (MainActivity)getActivity();
ma.startVideo("thelostegg");
My question is:

What is the proper way to close the _fragvideoplay from within the _fragvideoplay function which is preformed by clicking on the close FloatingActionButton?

closeVid.setOnClickListener(new View.OnClickListener() {
@override
public void onClick(View v) {
if (floatingBtnMenu.isOpened()) {
_stop();
getActivity().getFragmentManager().popBackStack();
}
}
});
I tried using the getActivity().getFragmentManager().popBackStack(); but that does not seem to get rid of the _fragvideoplay so that I am able to see the _fragmoviesfragment page again (The one with the button on it).

 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones