Two Fragments, two different layouts, one activity

add two fragments in one activity android
android multiple fragments in one activity example github
android multiple fragments in same container
change fragment layout dynamically
how to add fragment in activity in android example
android fragment vs activity
fragment activity
multiple xml in one activity in android

i have one activity, which runs fragment A at start in a RelativeLayout,

after that, inside this fragment, i try to open another fragment B, which is somehow an overlay only, it runs in another RelativeLayout, to look above fragment A, so far, all is working good, but the problem is in the onBackPressed():

I open the overlay fragment B, then i press back button, but it closes fragment A only.

Here is my activity's xml



I think this should explain it, but i'm up to provide any additional info.

When adding Fragments, you can add them to your backstack.

        .replace(, fragment)

Then you can simply check how many fragment are in your backstack and remove e.g. Fragment B when 2 when the backstack contains two fragments.

Creating and Using Fragments, Creating the First Fragment Layout. Right-click on the layout package under res and selecting the New → Other.. → Android → Android XML Layout → give the  Fragment is something like Frame in Html. One Html can contains multiple Frames, and one Activity can includes multiple Fragments. Each Fragment can has it’s own layout xml file. Contains it’s own view objects and encapsulate it’s own functionality. It can be reused by any activity.

You can use tags. Before opening fragment B, set the tag equal to for example "a". Override the onBackPressed() method and check the value of the tag. If it equal to "a" then open the fragment a. If it equal to "b" then open fragment b.

Two fragments in one activity, If in landscape mode the design as a single activity and two fragments. In the fragment app example we add fragments to the activity layout in two different  It is very common for your app to have many fragments inside an activity, especially apps for tablets. In this video, I will create one activity and put 2 layouts, RelativeLayout and LinearLayout

In the android documentation for Fragments it mentions this process.

If you don't call addToBackStack() when you perform a transaction that removes a fragment, then that fragment is destroyed when the transaction is committed and the user cannot navigate back to it. Whereas, if you do call addToBackStack() when removing a fragment, then the fragment is stopped and is later resumed if the user navigates back.

Dynamic Layouts using the Fragment Manager –, Two fragments, displayed in different configurations for the same activity on In order to replace one fragment with another, the activity's layout includes an  Say for instance, you include two layouts <include android:id = "@+id/layout1" layout = /> <include android:id = "@+id/layout2" layout = android:visibility = "gone"/> In your java code, you can then hide or show your layouts depending on the use case. For instance, setting the content view to show the layout above shows layout1.

Build a flexible UI, The 2 layouts are very important for Fragments. You will learn how to create a fragment and Duration: 10:50 Posted: Apr 10, 2017 A fragment represents a portion of user interface in an Activity, and provides a mechanism for creating reusable modules, after being created this module can be embedded within activities. In this example we will combine two fragments in an activity and follow the steps for the creation and use of fragments,

How to Use Multiple Fragments in One Activity, Fragments - Part 13, Supporting multiple layouts In this video of Fragments we explore the Duration: 11:41 Posted: Jan 28, 2018 How to use multiple fragment in single Activity. Here the demonstration Application which uses the two fragments one for showing month name and another for showing the details of month clicked in

Fragments - Part 13, Supporting multiple layouts, To display different fragments in your activities based Use one activity, which displays two fragments for in your layout and add the fragments at runtime to them. Essentially there are two activities and two different fragments used to implement this when the code runs in portrait mode; these activities are FragmentLayout and DetailsActivity. If you look at the code you will see that the new details fragment is drawn by the DetailsActivity using the same root view as defined in fragment_layout.xml above,

  • Add the complete java code of fragments as well as that activity.
  • the second fragment can't always be my fragment B, as i can open fragment C instead, and it'll still be the 2nd fragment in the stack after fragment A, but if i join your answer with what +Petros Masoyan said, with the tag, it can be done, thank you both so much ^_^
  • the documentation might help you. You can also add strings, but at the end it's just a simple calculation.