In Android Studio choose New Project → Empty Views Activity, with Language Java and Minimum SDK API 24.
2. Make room for fragments
In activity_main.xml, add a FragmentContainerView with the ID fragment_container. Fragments appear inside it. Below it, add two buttons: First and Second.
3. Create the first fragment
A fragment is a reusable piece of a screen, with its own layout. Create fragment_first.xml, then a FirstFragment class that extends Fragment and passes the layout to the constructor: super(R.layout.fragment_first). That's the whole class.
4. Create the second fragment
SecondFragment has its own button and counter. Find its views in onViewCreated, the fragment's version of an activity's onCreate.
5. Show the first fragment on launch
In MainActivity.onCreate, use getSupportFragmentManager().beginTransaction() to add the first fragment. Only do this when savedInstanceState is null; after a rotation, Android restores the fragment for you.
6. Swap fragments
When a button is tapped, replace the fragment in the container. addToBackStack(null) remembers the swap, so Back returns to the previous fragment. setCustomAnimations adds a fade.
7. Say "1 time", not "1 times"
The counter uses a plurals resource with one and other forms, read with getQuantityString. Android picks the right form for the number and the language.
8. Run it
Tap Second, tap the button a few times, then press Back to return to the first fragment.
If something goes wrong
Two fragments on top of each other after rotating: only add the first fragment when savedInstanceState == null.
Back closes the app instead of going back: add addToBackStack(null) to the transaction.
What's improved over the video
It uses FragmentContainerView, the layout-constructor shortcut, Back-stack support, fade animations and plurals.
Files affected
Check what to add and what to replace before copying code.
Adds the first fragment, and swaps fragments when the buttons are tapped.
MainActivity.java
packagecom.example.fragments;importandroid.os.Bundle;importandroid.widget.Button;importandroidx.appcompat.app.AppCompatActivity;importandroidx.fragment.app.Fragment;/** One screen with a container that shows either FirstFragment or SecondFragment. */publicclassMainActivityextendsAppCompatActivity{@OverrideprotectedvoidonCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);Screens.setUp(this,R.layout.activity_main);Button first =findViewById(R.id.btnFirst);Button second =findViewById(R.id.btnSecond);
first.setOnClickListener(v ->show(newFirstFragment()));
second.setOnClickListener(v ->show(newSecondFragment()));// Only add the first fragment on a fresh start. After rotation, Android restores the one on screen.if(savedInstanceState ==null){getSupportFragmentManager().beginTransaction().setReorderingAllowed(true).add(R.id.fragment_container,newFirstFragment()).commit();}}/** Swap the fragment in the container. addToBackStack lets Back return to the previous one. */privatevoidshow(Fragment fragment){getSupportFragmentManager().beginTransaction().setReorderingAllowed(true).setCustomAnimations(android.R.anim.fade_in,android.R.anim.fade_out,android.R.anim.fade_in,android.R.anim.fade_out).replace(R.id.fragment_container, fragment).addToBackStack(null).commit();}}