[Android / Java] Switch events by display Fragment

Introduction

There was a scene where I was developing an application with Android Studio (java) and wanted to implement "switching the process when the back button of the terminal is pressed according to the displayed fragment". I will post what I learned at that time.

What I learned

Described part of the code

Show Fragment from Activity

Show fragments in this activity xml file

activity_main.xml


<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <FrameLayout
        android:id="@+id/fl_activity_main"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

Fragment display processing is performed in this way

MainActivity.java


//A method to display the specified fragment. Pass the instance of the fragment you want to display in the argument fragment.
    public void showFragment(Fragment fragment, FragmentManager fragmentManager) {

        fragmentManager
                .beginTransaction()
                .replace(R.id.fl_activity_main, fragment)
                .addToBackStack(null)
                .commit();
    }

Get the displayed Fragment and perform an event (process)

This time, as an example, if the fragment displayed when the back button of the terminal is pressed is FooFragment or BarFragment, the activity is terminated.

MainActivity.java


// onBackPressed()Is a method called when the back button of the terminal is pressed
@Override
    public void onBackPressed() {
        //Get the fragment currently displayed here
        // findFragmentById()Replace the fragment display in the argument of()The id of the layout part to be displayed with (container))Put in.
        Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.fl_activity_main);
        
        //End activity when viewing FooFragment or BarFragment(finish)
        if (fragment instanceof FooFragment || fragment instanceof BarFragment) {
            finish();
        }
        super.onBackPressed();
    }

Finally

It was surprisingly easy to implement how to determine which fragment is being displayed. We will continue to actively output what we have learned.

Reference material

Thank you very much! !!

Android's first Fragment event

Recommended Posts

[Android / Java] Switch events by display Fragment
Seasonal display with Java switch
Screen transition by [Android Studio] [Java] button
Display text character by character in Android Surface View
Java switch statement
[Android / Java] Set up a button to return to Fragment
[Android] Review dialog display
I tried using the CameraX library with Android Java Fragment