Android/Java การเพิ่ม/ลบ Fragment ด้วย BackStack 1

เมื่อเปิดแอพจะสร้าง fragment ขึ้นมาด้วยเลย 1 อัน
ถ้าจะสร้าง fragment เพิ่มก็กดปุ่ม ADD FRAGMENT กด 1 ครั้งก็สร้าง fragment ขึ้่นมา 1 อัน (fragment ที่สร้างใหม่จะอยู่ด้านบน)
ถ้าจะลบ fragment ก็กดปุ่ม CLOSE FRAGMENT กด 1 ครั้งก็ลบ fragment ออกไป 1 อัน
หรือกด Back 1 ครั้งก็ลบ fragment ออกไป 1 อัน

สร้าง Fragment ขึ้นมาใหม่ชื่อ HomeFragment

ไฟล์ที่เกี่ยวข้อง

  • activity_main.xml
  • MainActivity.java
  • fragment_home.xml
  • HomeFragment.java

activity_main.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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Activity" />

        <Button
            android:id="@+id/btn_add_fragment"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Add Fragment" />

        <Button
            android:id="@+id/btn_remove_fragment"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Close Fragment" />

    </LinearLayout>

    <FrameLayout
        android:id="@+id/layout_fragment_container"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#d9e1ea" />

</LinearLayout>

MainActivity.java

package com.phaisarn.myapplication;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

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

public class MainActivity extends AppCompatActivity {
    private static final String TAG_HOME_FRAGMENT = "tag_home_fragment";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (savedInstanceState == null) {
            getSupportFragmentManager()
                    .beginTransaction()
                    .replace(R.id.layout_fragment_container,
                            HomeFragment.newInstance(),
                            TAG_HOME_FRAGMENT)
                    .addToBackStack(null)
                    .commit();
        }

        Button btnAddFragment = findViewById(R.id.btn_add_fragment);
        Button btnRemoveFragment = findViewById(R.id.btn_remove_fragment);

        btnAddFragment.setOnClickListener(onAddFragmentClickListener);
        btnRemoveFragment.setOnClickListener(onRemoveFragmentClickListener);
    }

    private View.OnClickListener onAddFragmentClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // คำสั่งเพิ่ม Fragment ลงบน ViewGroup
            getSupportFragmentManager()
                    .beginTransaction()
                    .replace(R.id.layout_fragment_container,
                            HomeFragment.newInstance(),
                            TAG_HOME_FRAGMENT)
                    .addToBackStack(null)
                    .commit();
        }
    };

    private View.OnClickListener onRemoveFragmentClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // คำสั่งลบ Fragment ที่อยู่บน ViewGroup
            Fragment fragment = getSupportFragmentManager().findFragmentByTag(TAG_HOME_FRAGMENT);
            if (fragment != null) {
                getSupportFragmentManager().popBackStack();
            }
        }
    };
}

fragment_home.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Home Fragment!" />

    <Button
        android:id="@+id/btn_close"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Close" />

</LinearLayout>

HomeFragment.java

package com.phaisarn.myapplication;


import android.os.Bundle;

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

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;


/**
 * A simple {@link Fragment} subclass.
 */
public class HomeFragment extends Fragment {

    public HomeFragment() {
        // Required empty public constructor
    }

    public static HomeFragment newInstance() {
        return new HomeFragment();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_home, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Button btnClose = view.findViewById(R.id.btn_close);
        btnClose.setOnClickListener(onCloseClickListener);
    }

    private View.OnClickListener onCloseClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            getFragmentManager().popBackStack();
        }
    };
}