安卓开发笔记Fragment详解(3)

  • Fragment回退栈

在AFragment文件中

package com.example.myapplication.Fragment;

import android.app.Activity;
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 android.widget.Button;
import android.widget.TextView;

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

import com.example.myapplication.R;

public class AFragment extends Fragment {

    private TextView mTvTitle;
    private Button mBtnChange,mBtnReset;
    private BFragment bFragment;



    public AFragment(){

    }


    //旧版写法
    public static AFragment newInstance(String title){
        AFragment aFragment = new AFragment();
        Bundle bundle = new Bundle();
        bundle.putString("title",title);
        aFragment.setArguments(bundle);
        return aFragment;
    }
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        //类似与在Fragment中setContentView()
        View view = inflater.inflate(R.layout.fragment_a,container,false);
        Log.d("AFragment","---AFragment---");
        return view;
    }



    //像findViewById这种操作就放到这里
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mTvTitle =view.findViewById(R.id.tv_title);
        mBtnChange = view.findViewById(R.id.btn_change);
        mBtnReset = view.findViewById(R.id.btn_reset);
        mBtnChange.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (bFragment == null){
                    bFragment = new BFragment();
                }
                Fragment fragment = getFragmentManager().findFragmentByTag("a");
                if (fragment != null){
                //先隐藏,再添加,这样回退时回收了表层的视图,底下的视图就可以看见了,也不需要重新加载浪费时间
                    getFragmentManager().beginTransaction().hide(fragment).add(R.id.f1_container,bFragment).addToBackStack(null).commitAllowingStateLoss();
                }
                else {
                    // addToBackStack可以使我们按下返回键的时候只返回上一层Fragment,不退回主页面,commit之前放入了回退栈
                    getFragmentManager().beginTransaction().replace(R.id.f1_container,bFragment).addToBackStack(null).commitAllowingStateLoss();
                }


            }
        });
        mBtnReset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mTvTitle.setText("我是新文字");
            }
        });

        if (getArguments()!=null){
            mTvTitle.setText(getArguments().getString("title"));
        }

//       mTvTitle.setText(title);

//        if (getActivity() !=null){
//
//        }else{
//
//        }


    }


}

视图如下:

<?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:orientation="vertical"
    android:gravity="center">
    <Button
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:id="@+id/btn_change"
        android:text="更换为BFragment"
        android:textAllCaps="false"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:id="@+id/btn_reset"
        android:text="更换TextView的文字内容"
        android:textAllCaps="false"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="@color/black"
        android:textSize="20sp"
        android:text="我是AFragment"
        android:gravity="center"
        android:id="@+id/tv_title"/>

</LinearLayout>

在这里插入图片描述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值