0

I am new to android development, please bear with me. I have three fragments - Frag1, Frag2 and Frag3. Frag1 is a sort of ListView having a list of items which is created when the App is launched. Frag2 and Frag3 basically correspond to VideoViews which will be used to play different videos from different players. One of them will play the content Video and the other will play Ad video.

When I click on a particular item in Frag1, I want to create both Frag2 and Frag3. I want to have both fragments since I might have to play the Ad at any time. I will hide the content Fragment when Ad is playing and Vice versa. Also I want that when back button is pressed, I should go back to Frag1 ( the listView), irrespective of whether Frag2 or Frag3 was visible when back button was pressed.

What I am not able to clearly figure out is how to stack the fragments so that whenever I press back button while playing either an Ad or video, it goes to the listView. Because if i understand correctly, the fragments get stacked up in the order they are added. So if I add in the order Frag1 followed by Frag2 and Frag3, then pressing back when Frag3 is on top will go to Frag2 rather than Frag1. Also say if currently Frag2 is visible and I press back, then I think Frag3 will still remain even though I want both Frag2 and Frag3 to get popped whenever I press back button.

Please help me regarding this. Any links which properly explains the concepts used here will also be appreciated.

Archit Sinha
  • 775
  • 1
  • 8
  • 33

3 Answers3

0

Fragments only get added to back stack if you call addToBackStack() while performing the Fragment Transaction.

Method 1

So while opening Frag2 from Frag1 you should call that because you want your Frag1 to be there when you press back.

Whereas when you open Frag3 from Frag2 or vice-versa, then don't call addToBackStack(). If then you press the back button from Frag3 or Frag2, you'll directly go to Frag1 as your back stack will only have Frag1.

Method 2

Another way is to override your onBackPressed() method in Activity. With this whenever the back button is pressed, you can pop the back stack using

Fragment f = getFragmentManager.popBackStackImmediate

And then check which Fragment is that by -

if(f instanceof Frag2 || f instanceof Frag3){
    //pop it
}

Now check again. Keep checking till only Frag1 is left.

Jyotman Singh
  • 10,792
  • 8
  • 39
  • 55
0

For this purpose you can use to tab fragment with below functionality. Try this one-

public void onBackPressed(){
   FragmentManager fm = getActivity().getSupportFragmentManager();
   fm.popBackStack();
}
Bhunnu Baba
  • 1,742
  • 15
  • 22
0

you need to manage the fragment by giving id to it , you can replace remove whatever you want in on backpress() , just look out the code that i develop to manage , me too had bit similar requirement so this is how i managed

    @Override
        public void onBackPressed() {

      Highlights_fragment test1 = (Highlights_fragment)getFragmentManager().findFragmentByTag("1");
      Aktulless_Fragment test2 = (Aktulless_Fragment)getFragmentManager().findFragmentByTag("2");

        if (test1 != null && test1.isVisible()) {
            //DO STUFF
            getFragmentManager().beginTransaction().remove(getFragmentManager().findFragmentById(R.id.frame_container)).commit();

        }
         else if(test2 != null && test2.isVisible()){
            //Whatever
            getFragmentManager().beginTransaction().remove(getFragmentManager().findFragmentById(R.id.frame_container)).commit();

        }
        else
        {
            super.onBackPressed();
        }

and for Frag2 and Frag3 you need to replace the particular fragment on particular item pressed.

Hope it helps :)

Pratik
  • 456
  • 4
  • 18