[Android / Java] Set up a button to return to Fragment

Introduction

In the installation of the back button on the fragment I used to define it in the parent Activity and call it. It is better to call the contents directly from each Fragment without putting it in Activity As a veteran engineer pointed out, I will leave both patterns as a memorandum.

① How to call from MainActivity

MainActivity.java


public class MainActivity extends AppCompatActivity {

//Define back button
    public void setupBackButton(boolean enableBackButton) {
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(enableBackButton);
    }
}

Fragment1.java


 //Call in onCreateView
 MainActivity activity = (MainActivity) getActivity();
 activity.setupBackButton(true);
 setHasOptionsMenu(true);  //Settings required to use ActionBar with Fragment

Fragment2.java


 //Call in onCreateView
 MainActivity activity = (MainActivity) getActivity();
 activity.setupBackButton(false);

② How to call directly from Fragment

Fragment1.java


 //Call in onCreateView
 .setupBackButton()
 setHasOptionsMenu(true);  //Settings required to use ActionBar with Fragment
 
 //Defined in Fragment class
 public void setupBackButton() {
        AppCompatActivity activity = (AppCompatActivity) getActivity();
        ActionBar actionBar = activity.getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
  }

Fragment2.java


 
 //Call in onCreateView
 .hideBackButton()

 //Defined in Fragment class
 public void hideBackButton() {
        AppCompatActivity activity = (AppCompatActivity) getActivity();
        ActionBar actionBar = activity.getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(false);
  }

the difference

It looks the same at first glance, but the source of Activity is different! ①MainActivity activity = (MainActivity) getActivity(); ②AppCompatActivity activity = (AppCompatActivity) getActivity();

It is said that it is better to stop writing on the premise of a specific activity (Main Activity in this case). This is because the more prerequisite classes (classes you define), the harder it is for your code to make safe changes.

If the acquisition destination is AppCompatActivity, it is an inheritance source, so it is almost never changed and it is highly safe. I want to be a strong engineer who can implement with a view to the future.

Recommended Posts

[Android / Java] Set up a button to return to Fragment
Android Easily give a "press" to a button
Set up a Java GUI in a separate thread to keep the main
Steps to set up a VNC server on CentOS 8.3
How to set up Android OR mapper "Orma" (Kotlin)
How to set Java constants
Create a method to return the tax rate in Java
How to set up a proxy with authentication in Feign
How to perform a specific process when the back button is pressed in Android Fragment
Android Development-Try to display a dialog-
[Ubuntu] Set up a Nukkit server
[Java] How to create a folder
[Wire Mock] I want to set up a stub / mock server in Java and perform E2E tests.
[Android] Convert Android Java code to Kotlin
How to make a Java array
[Android] How to make Dialog Fragment
Minimal steps to set up a Ruby environment with rbenv on Ubuntu 20.04
How to make a Java calendar Summary
Convert all Android apps (Java) to Kotlin
[Introduction to Java] How to write a Java program
How to make a Discord bot (Java)
How to set up and use kapt
[Java] How to set the Date time to 00:00:00
Steps to set a favicon in Rails
How to print a Java Word document
Screen transition by [Android Studio] [Java] button
[Android / Java] Switch events by display Fragment
[Ruby on Rails] Button to return to top
Java to C and C to Java in Android Studio
Java (set)
A note about adding Junit 4 to Android Studio
Two ways to start a thread in Java + @
[Rails] Button to return to the top of the page
A memorandum to clean up the code Ruby
[Android] How to convert a character string to resourceId
Connect to Aurora (MySQL) from a Java application
How to display a web page in Java
java I tried to break a simple block
To become a VB.net programmer from a Java shop
[Android] How to detect volume change (= volume button press)
Code to escape a JSON string in Java
I did Java to make (a == 1 && a == 2 && a == 3) always true
Try to create a bulletin board in Java
Set the time of LocalDateTime to a specific time
[Android] Implement a function to display passwords quickly
I wanted to make (a == 1 && a == 2 && a == 3) true in Java
How to convert a solidity contract to a Java contract class
A story about trying to operate JAVA File
[Java] (for MacOS) How to set the classpath
How to set up and operate jEnv (Mac)
Java adds a text box to PowerPoint slides
Use CarrierWave to set a user's profile picture
How to set up JavaED Full Edition (pleiades)
[Android] Two ways to get a Bluetooth Adapter
[Rails] How to create a Twitter share button
[Java] 4 steps to implement splash screen on Android
[Android] Correct order to set Context in Dao
Set up a webhook in Shopify's custom app
Set up a CentOS virtual server with Vagrant
I tried to break a block with java (1)
Bitmap too large to be uploaded into a texture trying to set a large Bitmap in Android ImageView