iT邦幫忙

2022 iThome 鐵人賽

DAY 24
0
Mobile Development

Android Studio - 30天菜鳥筆記系列 第 24

Android Studio菜鳥筆記 - Day24 - Fragment之add()、show()、hide()使用方法

  • 分享至 

  • xImage
  •  

每次切換Fragment重新使用replace方法,Fragment的生命週期就會重頭開始跑,導致其一個Fragment被銷毀
這時我麼可以使用add()、show()、hide()方法,來保存Fragment讓其可以重複被使用,不用重新new一個
範例:有三個Fragment分別為fragment_one、fragment_two、fragment_three互相切換
下方是要叫出fragment_one的程式碼

一開始要先判斷fragment_one是否有被創建,若沒有就創建

if (fragment_one == null){
    fragment_one = new Fragment_one();
}

再來判斷Fragment_one外的其他Fragment是否有值(有無被創建),若有值則把其隱藏以免影響Fragment顯示

if (fragment_two != null){
    fragmentTransaction.hide(fragment_two);
}
if (fragment_three != null){
    fragmentTransaction.hide(fragment_three);
}

最後是判斷有沒有被Add過,有Add才可以show出來

if (fragment_two.isAdded()){
    fragmentTransaction.show(fragment_two);
} else{
    fragmentTransaction.add(R.id.fragmentContainerView, fragment_two).show(fragment_two);
}

記得最後要commit()

MainActivity.java

package com.example.fragment_demo;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

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

public class MainActivity extends AppCompatActivity {
    private Fragment_one fragment_one;
    private Fragment_two fragment_two;
    private Fragment_three fragment_three;
    private FragmentManager fragmentManager;
    private FragmentTransaction fragmentTransaction;
    Button bt_f1, bt_f2, bt_f3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bt_f1 = findViewById(R.id.bt_f1);
        bt_f2 = findViewById(R.id.bt_f2);
        bt_f3 = findViewById(R.id.bt_f3);
        fragmentManager = getSupportFragmentManager();
        bt_f1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                fragmentTransaction = fragmentManager.beginTransaction();
                if (fragment_one == null){
                    fragment_one = new Fragment_one();
                }
                if (fragment_two != null){
                    fragmentTransaction.hide(fragment_two);
                }
                if (fragment_three != null){
                    fragmentTransaction.hide(fragment_three);
                }
                if (fragment_one.isAdded()){
                    fragmentTransaction.show(fragment_one);
                } else{
                    fragmentTransaction.add(R.id.fragmentContainerView, fragment_one).show(fragment_one);
                }
                fragmentTransaction.commit();
            }
        });
        bt_f2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                fragmentTransaction = fragmentManager.beginTransaction();
                if (fragment_two == null){
                    fragment_two = new Fragment_two();
                }
                if (fragment_one != null){
                    fragmentTransaction.hide(fragment_one);
                }
                if (fragment_three != null){
                    fragmentTransaction.hide(fragment_three);
                }

                if (fragment_two.isAdded()){
                    fragmentTransaction.show(fragment_two);
                } else{
                    fragmentTransaction.add(R.id.fragmentContainerView, fragment_two).show(fragment_two);
                }
                fragmentTransaction.commit();
            }
        });
        bt_f3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                fragmentTransaction = fragmentManager.beginTransaction();
                if (fragment_three == null){
                    fragment_three = new Fragment_three();
                }
                if (fragment_one != null){
                    fragmentTransaction.hide(fragment_one);
                }
                if (fragment_two != null){
                    fragmentTransaction.hide(fragment_two);
                }

                if (fragment_three.isAdded()){
                    fragmentTransaction.show(fragment_three);
                } else{
                    fragmentTransaction.add(R.id.fragmentContainerView, fragment_three).show(fragment_three);
                }
                fragmentTransaction.commit();
            }
        });
    }
}

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">

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/fragmentContainerView"
        android:layout_width="match_parent"
        android:layout_height="600dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/bt_f1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginBottom="16dp"
        android:text="F1"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/bt_f2"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:id="@+id/bt_f3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:layout_marginBottom="16dp"
        android:text="f3"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/bt_f2" />

    <Button
        android:id="@+id/bt_f2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:text="f2"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

上一篇
Android Studio菜鳥筆記 - Day23 - Fragment基本介紹及replace()用法
下一篇
Android Studio菜鳥筆記 - Day25 - BottomNavigationView + Fragment
系列文
Android Studio - 30天菜鳥筆記30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言