Skip to content

Fragments ​

So far every screen you built was one Activity. That works on a phone, but on a tablet one screen can show a list and its details side by side. A Fragment is a reusable piece of screen with its own layout and its own code, which lives inside an Activity. One Activity can host several Fragments, and the same Fragment can be used in several Activities.

In this lab you build one screen with three Fragments. A green Fragment at the top has a button. Below it is an empty box, and five buttons add, remove, replace, detach and attach a Fragment in that box. Whichever Fragment is in the box shows how many times the green button was clicked, so the Fragments share data without knowing about each other. Every Fragment also writes its lifecycle to Logcat, so you can see what each operation does.

Objectives ​

  • Create Fragments and put one in an Activity's layout with FragmentContainerView.
  • Add, remove, replace, detach and attach Fragments in code with a FragmentTransaction.
  • Share data between Fragments with a ViewModel and LiveData.
  • Read a Fragment's lifecycle callbacks in Logcat, and know what survives a screen rotation.

Background ​

What a Fragment is ​

A Fragment is a class that extends androidx.fragment.app.Fragment and has a layout. It is not a screen on its own: it always sits inside an Activity, and when the Activity is destroyed, its Fragments go with it.

java
public class ButtonFragment extends Fragment {
    public ButtonFragment() {
        super(R.layout.fragment_button);   // the layout this Fragment shows
    }
}

There are two ways to put a Fragment in an Activity. This lab uses both:

StaticDynamic
HowA FragmentContainerView in the Activity's XML names the Fragment class with android:nameAn empty FragmentContainerView, and Java code that runs a transaction to put a Fragment in it
Can be removed or swappedNoYes
In this labButtonFragment, the green barMessageFragment and NumberFragment, in the box

Fragment transactions ​

The FragmentManager keeps track of the Fragments in an Activity. You get it with getSupportFragmentManager(), and you change its Fragments with a transaction: begin it, say what to do, and commit it.

java
getSupportFragmentManager().beginTransaction()
        .add(R.id.fragment_container, new MessageFragment())
        .commit();
OperationWhat happens to the FragmentIts view
add(container, fragment)Created and put in the containerShown
remove(fragment)Destroyed and taken out of the managerGone
replace(container, fragment)The Fragment already in the container is removed, then this one is addedSwapped
detach(fragment)Kept in the manager, but its view is destroyedGone
attach(fragment)A detached Fragment gets a new viewShown

To get a Fragment back later, ask the manager for it by its container's id with findFragmentById(R.id.fragment_container). You can also give a Fragment a name, a tag, when you add it (add(container, fragment, "message")) and find it with findFragmentByTag("message").

The lifecycle ​

A Fragment has a lifecycle like an Activity's, with extra steps, because its view can be destroyed and recreated while the Fragment object stays alive:

WhenCallbacks, in order
AddedonAttach → onCreate → onCreateView → onViewCreated → onStart → onResume
DetachedonPause → onStop → onDestroyView
Attached againonCreateView → onViewCreated → onStart → onResume
RemovedonPause → onStop → onDestroyView → onDestroy → onDetach

Code that uses the Fragment's views goes in onViewCreated, because the views exist only between onViewCreated and onDestroyView.

Sharing data between Fragments ​

Fragments should not call each other directly. If ButtonFragment held a reference to MessageFragment, it would break as soon as the box held a NumberFragment instead, or nothing at all. Android has three ways for Fragments to share data:

WayHow it worksUse it for
Shared ViewModel (this lab)Both Fragments get the same ViewModel object from their Activity. One changes the data, the others observe it.Data several Fragments show, which should survive a rotation
Fragment Result APIOne Fragment sends a Bundle with setFragmentResult, another receives it with setFragmentResultListenerHanding one result back once, such as a choice made in a dialog
Interface through the Activity (the manual)The Fragment calls a method on its Activity, and the Activity calls a method on the other FragmentOlder code. The Activity has to hold both Fragments together.

A ViewModel is a class that holds a screen's data. Android keeps it alive while its owner, here the Activity, is on screen, including through rotations, which destroy and recreate the Activity and its Fragments. Each Fragment asks for it with:

java
CounterViewModel counter = new ViewModelProvider(requireActivity()).get(CounterViewModel.class);

requireActivity() makes the Activity the owner. The first call creates the ViewModel, and every later call from any Fragment in that Activity returns the same object.

The data inside it is a LiveData, a value that others can observe:

java
counter.getCount().observe(getViewLifecycleOwner(), count -> textView.setText(…));

The lambda runs at once with the current value, then again each time the value changes. getViewLifecycleOwner() ties the observer to the Fragment's view: when the view is destroyed, the observer is removed automatically, so a detached or removed Fragment never tries to update a view it no longer has.

Step 1: Create the project ​

Create a new project as described in Setup Guide → Create a lab project:

FieldValue
TemplateEmpty Views Activity
NameExp7Fragments
Package nameedu.birzeit.encs5150.exp7
LanguageJava
Minimum SDKAPI 24

ViewModel, LiveData and the Fragment classes all come with the template's appcompat library, so you don't add any dependencies.

Step 2: The shared ViewModel ​

  1. Right-click the package edu.birzeit.encs5150.exp7 → New → Java Class.
  2. Name it CounterViewModel and press Enter.
  3. Replace the file's contents with:
java
package edu.birzeit.encs5150.exp7;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

// The data the Fragments share: how many times the button was clicked
public class CounterViewModel extends ViewModel {

    private final MutableLiveData<Integer> count = new MutableLiveData<>(0);

    // Read-only for the Fragments that show the count
    public LiveData<Integer> getCount() {
        return count;
    }

    public void increment() {
        count.setValue(count.getValue() + 1);
    }
}
  • count is a MutableLiveData<Integer> that starts at 0. Only this class can change it.
  • getCount() returns it as a plain LiveData, which has no setValue, so the Fragments that show the count can observe it but can't change it.
  • increment() is the only way to change it: it reads the current value, adds one, and sets it. setValue tells every observer.

Step 3: A Fragment that logs its lifecycle ​

Create another Java class, LoggedFragment, and replace its contents with:

java
package edu.birzeit.encs5150.exp7;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

// A Fragment that writes each lifecycle callback to Logcat
public abstract class LoggedFragment extends Fragment {

    private static final String TAG = "FragmentLifecycle";

    public LoggedFragment(@LayoutRes int layoutId) {
        super(layoutId);
    }

    private void log(String callback) {
        Log.d(TAG, getClass().getSimpleName() + ": " + callback);
    }

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        log("onAttach");
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        log("onCreate");
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        log("onCreateView");
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        log("onViewCreated");
    }

    @Override
    public void onStart() {
        super.onStart();
        log("onStart");
    }

    @Override
    public void onResume() {
        super.onResume();
        log("onResume");
    }

    @Override
    public void onPause() {
        super.onPause();
        log("onPause");
    }

    @Override
    public void onStop() {
        super.onStop();
        log("onStop");
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        log("onDestroyView");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        log("onDestroy");
    }

    @Override
    public void onDetach() {
        super.onDetach();
        log("onDetach");
    }
}

It overrides each lifecycle callback only to write the callback's name to Logcat, with the tag FragmentLifecycle, then calls super. The three Fragments in this lab extend it, so you get the log without writing it three times. It is abstract because it is a base class, never shown by itself.

Step 4: Create the Fragments ​

  1. Right-click the package → New → Fragment → Fragment (Blank).
  2. Name it ButtonFragment and click Finish.
  3. Do the same for MessageFragment and NumberFragment.

Each time, the wizard creates a Java class and a layout (fragment_button.xml, fragment_message.xml, fragment_number.xml) with some example code. You will replace all six files.

Step 5: The Fragment layouts ​

Replace the whole res/layout/fragment_button.xml with:

xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#2E7D32"
    android:padding="16dp"
    tools:context=".ButtonFragment">

    <Button
        android:id="@+id/button_count"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Click me" />

</FrameLayout>

The whole fragment_message.xml with:

xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#0277BD"
    android:padding="16dp"
    tools:context=".MessageFragment">

    <TextView
        android:id="@+id/textView_message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:textAppearance="@style/TextAppearance.AppCompat.Large"
        android:textColor="#FFFFFF"
        tools:text="The button was clicked 0 times" />

</FrameLayout>

And the whole fragment_number.xml with:

xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#EF6C00"
    android:padding="16dp"
    tools:context=".NumberFragment">

    <TextView
        android:id="@+id/textView_number"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:textAppearance="@style/TextAppearance.AppCompat.Display3"
        android:textColor="#FFFFFF"
        tools:text="0" />

</FrameLayout>

The button Fragment is a green bar with layout_height="wrap_content", so it takes only the height it needs. The other two fill the box (match_parent): a blue one with a sentence and an orange one with a big number. Their TextViews have no android:text, only tools:text, which shows in the Design view but not in the app, because the code sets the real text.

Step 6: The Fragment code ​

Replace the whole ButtonFragment.java with:

java
package edu.birzeit.encs5150.exp7;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProvider;

public class ButtonFragment extends LoggedFragment {

    public ButtonFragment() {
        super(R.layout.fragment_button);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        // The Activity's ViewModel, shared with every Fragment in it
        CounterViewModel counter = new ViewModelProvider(requireActivity()).get(CounterViewModel.class);

        Button countButton = view.findViewById(R.id.button_count);
        countButton.setOnClickListener(v -> counter.increment());
    }
}
  1. extends LoggedFragment and super(R.layout.fragment_button): the Fragment logs its lifecycle and shows fragment_button.xml. There is no onCreateView to write.
  2. onViewCreated runs once the layout exists. It gets the shared CounterViewModel from the Activity, finds the button with view.findViewById, and makes it call counter.increment().

ButtonFragment doesn't know which Fragment shows the count, or whether any does.

Replace the whole MessageFragment.java with:

java
package edu.birzeit.encs5150.exp7;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProvider;

public class MessageFragment extends LoggedFragment {

    public MessageFragment() {
        super(R.layout.fragment_message);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        CounterViewModel counter = new ViewModelProvider(requireActivity()).get(CounterViewModel.class);
        TextView messageTextView = view.findViewById(R.id.textView_message);

        // Runs now with the current count, then again every time it changes
        counter.getCount().observe(getViewLifecycleOwner(), count ->
                messageTextView.setText("The button was clicked " + count + " times"));
    }
}

It gets the same CounterViewModel and observes getCount(). Each time the count changes, the lambda puts it in the sentence. Because the lambda also runs right away with the current value, a MessageFragment added after ten clicks shows "10 times" immediately.

Replace the whole NumberFragment.java with:

java
package edu.birzeit.encs5150.exp7;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProvider;

public class NumberFragment extends LoggedFragment {

    public NumberFragment() {
        super(R.layout.fragment_number);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        CounterViewModel counter = new ViewModelProvider(requireActivity()).get(CounterViewModel.class);
        TextView numberTextView = view.findViewById(R.id.textView_number);

        counter.getCount().observe(getViewLifecycleOwner(), count ->
                numberTextView.setText(String.valueOf(count)));
    }
}

It is the same as MessageFragment, but it shows only the number. String.valueOf(count) turns it into text. Never write setText(count) with an int: setText would read it as the id of a string resource and crash.

Step 7: The Activity layout ​

Replace the whole res/layout/activity_main.xml with:

xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <!-- Static: this Fragment is always here -->
    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/fragment_button"
        android:name="edu.birzeit.encs5150.exp7.ButtonFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginHorizontal="8dp"
        android:layout_marginTop="8dp"
        android:orientation="horizontal">

        <Button
            android:id="@+id/button_add"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="4dp"
            android:layout_weight="1"
            android:text="Add" />

        <Button
            android:id="@+id/button_remove"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginHorizontal="4dp"
            android:layout_weight="1"
            android:text="Remove" />

        <Button
            android:id="@+id/button_replace"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="4dp"
            android:layout_weight="1"
            android:text="Replace" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginHorizontal="8dp"
        android:orientation="horizontal">

        <Button
            android:id="@+id/button_detach"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="4dp"
            android:layout_weight="1"
            android:text="Detach" />

        <Button
            android:id="@+id/button_attach"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="4dp"
            android:layout_weight="1"
            android:text="Attach" />
    </LinearLayout>

    <!-- Dynamic: empty at first; the buttons above put Fragments here -->
    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_margin="8dp"
        android:layout_weight="1" />

</LinearLayout>

From top to bottom:

  • fragment_button is a static FragmentContainerView. Its android:name is the full class name of the Fragment, package included, and Android creates a ButtonFragment in it when the layout is inflated.
  • Two rows of buttons. Each button has layout_width="0dp" and layout_weight="1", so the buttons in a row share its width equally.
  • fragment_container is a dynamic FragmentContainerView: it has no android:name, so it starts empty. layout_height="0dp" with layout_weight="1" makes it take the rest of the screen.

Step 8: The Activity code ​

Open MainActivity.java and replace the whole file with the code below. The template lines at the top of onCreate stay as they were.

java
package edu.birzeit.encs5150.exp7;

import android.os.Bundle;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;

public class MainActivity extends AppCompatActivity {

    private FragmentManager fragmentManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        fragmentManager = getSupportFragmentManager();

        findViewById(R.id.button_add).setOnClickListener(v -> addFragment());
        findViewById(R.id.button_remove).setOnClickListener(v -> removeFragment());
        findViewById(R.id.button_replace).setOnClickListener(v -> replaceFragment());
        findViewById(R.id.button_detach).setOnClickListener(v -> detachFragment());
        findViewById(R.id.button_attach).setOnClickListener(v -> attachFragment());
    }

    private void addFragment() {
        if (fragmentManager.findFragmentById(R.id.fragment_container) != null) {
            Toast.makeText(this, "The box already has a fragment", Toast.LENGTH_SHORT).show();
            return;
        }
        fragmentManager.beginTransaction()
                .add(R.id.fragment_container, new MessageFragment())
                .commit();
    }

    private void removeFragment() {
        Fragment current = findCurrentOrWarn();
        if (current != null) {
            fragmentManager.beginTransaction().remove(current).commit();
        }
    }

    private void replaceFragment() {
        Fragment current = findCurrentOrWarn();
        if (current == null) {
            return;
        }
        if (current.isDetached()) {
            Toast.makeText(this, "Attach the fragment first", Toast.LENGTH_SHORT).show();
            return;
        }
        // Swap a MessageFragment for a NumberFragment, and back
        Fragment next = (current instanceof MessageFragment) ? new NumberFragment() : new MessageFragment();
        fragmentManager.beginTransaction()
                .replace(R.id.fragment_container, next)
                .commit();
    }

    private void detachFragment() {
        Fragment current = findCurrentOrWarn();
        if (current != null) {
            fragmentManager.beginTransaction().detach(current).commit();
        }
    }

    private void attachFragment() {
        Fragment current = findCurrentOrWarn();
        if (current != null) {
            fragmentManager.beginTransaction().attach(current).commit();
        }
    }

    // The Fragment in the box (attached or detached), or null with a Toast if the box is empty
    private Fragment findCurrentOrWarn() {
        Fragment current = fragmentManager.findFragmentById(R.id.fragment_container);
        if (current == null) {
            Toast.makeText(this, "Add a fragment first", Toast.LENGTH_SHORT).show();
        }
        return current;
    }
}
  1. fragmentManager is getSupportFragmentManager(), the manager for this Activity.
  2. Each button runs one method, and each method is one transaction: beginTransaction(), one operation, commit().
  3. findCurrentOrWarn asks the manager for the Fragment in the box with findFragmentById(R.id.fragment_container). This finds a detached Fragment too, since it is still in the manager. If the box is empty, it shows a Toast and returns null, and the caller does nothing.
  4. addFragment creates a new MessageFragment each time, but only if the box is empty.
  5. replaceFragment looks at the Fragment in the box: a MessageFragment is replaced by a new NumberFragment, and the other way round. It refuses to replace a detached Fragment, because replace only removes the Fragments that are attached, and the detached one would stay behind in the manager.

The Activity only moves Fragments around. It plays no part in passing the count, which goes through the ViewModel.

Run and check ​

Open Logcat in Android Studio and type FragmentLifecycle in the search box. When the app starts, ButtonFragment logs onAttach through onResume, and the box is empty.

  • Press Remove, Replace, Detach or Attach while the box is empty. A Toast says "Add a fragment first".
  • Press Click me three times, then Add. The blue box appears and says "The button was clicked 3 times": the new Fragment read the current count the moment it started observing.
  • Press Add again. A Toast says "The box already has a fragment".
  • Press Click me a few more times. The blue text follows each click.

Now go through the table and compare Logcat with the lifecycle:

PressOn screenLogcat
DetachThe blue box disappearsMessageFragment: onPause, onStop, onDestroyView
Click me twiceNothing changesNothing: the detached Fragment has no view, so it has no observer
AttachThe blue box is back, with the count including the two clicksonCreateView, onViewCreated, onStart, onResume. No onAttach or onCreate: the Fragment was never destroyed.
ReplaceThe orange box shows the same count as a numberThe old Fragment stops (onPause, onStop), the new one is created (onAttach … onStart), then the old one is destroyed (onDestroyView, onDestroy, onDetach) and the new one resumes (onResume)
Replace againBack to the blue sentenceThe same, with the two Fragments swapped
RemoveThe box is emptyonPause, onStop, onDestroyView, onDestroy, onDetach
AddThe blue box, still with the right countA brand-new MessageFragment: onAttach … onResume

Detach and Remove look the same on screen, but Logcat shows the difference. After Detach, the Fragment object is still alive (onDestroy was not called), and Attach only gives it a new view. After Remove, it is gone, and Add creates a new one.

Finally, with a Fragment in the box, rotate the emulator (the rotate buttons in the emulator's toolbar). Logcat shows every Fragment destroyed and then created again, because rotating recreates the Activity. The FragmentManager restores the Fragment that was in the box, and the count is unchanged, because the ViewModel survives the rotation.

Differences from the manual ​

The manual builds two apps, connects its Fragments with an interface through the Activity, and uses several APIs that Android Studio now marks as deprecated. Here is what changed and why.

1. One app instead of two ​

The manual builds a "Fragments Communication Application" with two static Fragments, and a separate "Fragments Transaction Application" that adds Fragments in code. This guide puts both on one screen: the static ButtonFragment sends the count, and the Fragments you add, replace and remove show it. That saves a second project setup. It also shows communication working with a Fragment that comes and goes, which the manual's apps never try.

2. A shared ViewModel instead of an interface ​

java
// SecondFragment
interface communicator {
    public void respond(String data);
}

// MainActivity implements SecondFragment.communicator
@Override
public void respond(String data) {
    SecondFragment secondFragment =
            (SecondFragment) getSupportFragmentManager().findFragmentById(R.id.fragment2);
    secondFragment.changeData(data);
}

// FirstFragment
final SecondFragment.communicator communicator =
        (SecondFragment.communicator) getActivity();
… communicator.respond("the button is clicked " + i[0] + " times");
java
// ButtonFragment
counter.increment();

// MessageFragment and NumberFragment
counter.getCount().observe(getViewLifecycleOwner(), count -> …);

The manual's interface works, but it is the older pattern. Android's current guide to Fragment communication recommends a shared ViewModel for data like this, and the Fragment Result API for one-off results. For this app the ViewModel is better:

  • The Activity is no longer the middleman. In the manual, MainActivity has to implement the interface, find the second Fragment, cast it and call it. Here it does none of that.
  • The receiving Fragment doesn't have to exist. The manual's respond assumes SecondFragment is on screen. If it had been removed, findFragmentById would return null and the app would crash. Here, a Fragment that isn't there simply isn't observing, and one added later reads the current count.
  • The count survives rotation. The manual keeps the count in a local final int[] i in the Fragment, so it starts again from 0 when the screen rotates. The ViewModel keeps it.
  • The data has one owner. The manual sends a ready-made sentence, so the receiver can't show the count any other way. Here the ViewModel holds the number, and each Fragment decides how to show it.

The manual's interface is also defined inside SecondFragment, the Fragment that receives the data, although FirstFragment calls it, and it is named communicator in lower case, against Java's naming convention.

3. FragmentContainerView instead of <fragment> ​

xml
<fragment
    android:id="@+id/fragment1"
    android:name="….FirstFragment" … />
xml
<androidx.fragment.app.FragmentContainerView
    android:id="@+id/fragment_button"
    android:name="edu.birzeit.encs5150.exp7.ButtonFragment" … />

The manual drags a <fragment> tag from the palette. Android Studio now flags that tag and recommends FragmentContainerView, which handles Fragment transactions and animations correctly. It is also the right container for Fragments added in code, so this guide uses it for the box too, where the manual adds Fragments to its root LinearLayout.

4. onViewCreated instead of onActivityCreated ​

java
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    …
    Button button = (Button) getActivity().findViewById(R.id.button);
java
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    …
    Button countButton = view.findViewById(R.id.button_count);

onActivityCreated is deprecated: a Fragment shouldn't depend on when its Activity finishes onCreate. Code that sets up the Fragment's views now goes in onViewCreated, which gets the Fragment's own view as a parameter.

The manual also finds views with getActivity().findViewById, which searches the whole Activity's layout. If two Fragments had a view with the same id, it would find whichever comes first, and it returns null if the Fragment's view isn't on screen. view.findViewById searches only this Fragment's layout.

5. A new Fragment for each add ​

java
final FirstFragment firstFragment = new FirstFragment();   // created once, in onCreate

fragmentTransaction.add(R.id.root_layout, firstFragment, "FristFrag");
java
fragmentManager.beginTransaction()
        .add(R.id.fragment_container, new MessageFragment())
        .commit();
// later: fragmentManager.findFragmentById(R.id.fragment_container)

The manual creates one Fragment object in onCreate and keeps it for every Add. Adding that same object a second time throws IllegalStateException: Fragment already added. It also goes wrong after a rotation: Android restores the Fragments the manager had, but the new Activity creates another object, so its variable no longer points at the Fragment on screen. This guide creates a new Fragment for each add and always asks the manager for the one that is really in the box.

6. All five operations, and a log ​

The manual shows only add and leaves "remove, attach, detach and replace" to you, without explaining how they differ. This guide does all five, and the lifecycle log shows what each one does to the Fragment.

7. Smaller changes ​

ManualThis guideWhy
FirstFragment, SecondFragmentButtonFragment, MessageFragment, NumberFragmentEach name says what the Fragment shows.
Override onCreateView (what the wizard generates)super(R.layout.fragment_button) in the constructorOne line instead of a method.
final int[] i = {0} to count clickscount in CounterViewModelThe array was a workaround to change a number inside an anonymous class, and lost the count on rotation.
new View.OnClickListener() { … }A lambda, v -> …Shorter.
getFragmentManager() in the textgetSupportFragmentManager()getFragmentManager() is deprecated. The manual's own code already uses the support one.
@android:color/holo_green_dark and holo_blue_light#2E7D32, #0277BD, #EF6C00Colours that are readable with white text.
Layouts built from screenshotsPaste the full XMLEveryone gets an identical layout with the right ids.

Common problems ​

SymptomCause and fix
The box never shows the clicks: it stays at 0The Fragments use new ViewModelProvider(this). In a Fragment, this makes the Fragment the owner, so each Fragment gets its own CounterViewModel. Use requireActivity() so they share the Activity's.
InflateException with ClassNotFoundException for ButtonFragmentThe android:name in activity_main.xml isn't the full class name. It needs the package: edu.birzeit.encs5150.exp7.ButtonFragment.
IllegalArgumentException: No view found for id … for fragment MessageFragmentThe transaction uses an id that is not in activity_main.xml. Use R.id.fragment_container.
IllegalStateException: Fragment already addedThe same Fragment object was added twice. Create it with new for each add.
Resources$NotFoundException: String resource ID #0x3setText was given an int. Turn the number into text first: String.valueOf(count).
Android Studio underlines observe(this, …) in a FragmentObserve with getViewLifecycleOwner(), not this. With this, the observer outlives a detached Fragment's view, and each Attach adds another one.
NullPointerException in incrementcount was created with new MutableLiveData<>(), which starts as null. Give it a starting value: new MutableLiveData<>(0).
Logcat shows nothingType FragmentLifecycle in Logcat's search box, and make sure the log level isn't set above Debug.
A button does nothingIts findViewById(...).setOnClickListener line in MainActivity is missing, or uses the wrong R.id.

Things to try ​

  • Add .addToBackStack(null) before .commit() in replaceFragment. Now the system Back button undoes a replace instead of closing the app.
  • Use hide and show instead of detach and attach, and compare the lifecycle in Logcat. Which callbacks run?
  • Add a Reset button to NumberFragment that calls a new reset() method in CounterViewModel. The MessageFragment doesn't need to change.
  • Make a tablet layout: copy activity_main.xml into a new res/layout-sw600dp/ folder and put the box beside the button bar instead of under it. Run it on a tablet emulator.
  • Rewrite the communication with the Fragment Result API: ButtonFragment calls getParentFragmentManager().setFragmentResult(…) and MessageFragment listens with setFragmentResultListener. What happens to the count when you rotate, or when you add the Fragment after a few clicks?