addOnBackStackChangedListener没有在我的android片段中触发

问题描述 投票:0回答:1

我是Android开发的新手,我正在使用FragmentaddOnBackStackChangedListener来跟踪我的后台堆栈更改,但它没有收听。这是我的完整代码,每个按钮都有效。只是它不捕获后栈更改事件

package com.practice.personal.fragdemo;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    FragmentA fa;
    FragmentB fb;
    FragmentManager mgr;
    FragmentTransaction tx;
    TextView txt;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        mgr=getFragmentManager();
        txt=findViewById(R.id.message);
        mgr.addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
            @Override
            public void onBackStackChanged() {
                FragmentManager.BackStackEntry entry;
                int c= mgr.getBackStackEntryCount();
                for(int i=c-1;i>c;i--){
                    entry=mgr.getBackStackEntryAt(i);
                    Log.d("ABD",txt.getText()+entry.getName()+"\n");
                    txt.setText(txt.getText()+entry.getName()+"\n");
                }
            }
        });


    }
    public void addA(View view){
        fa = new FragmentA();
        tx=mgr.beginTransaction();
        tx.add(R.id.group, fa,"A");
        tx.addToBackStack("A");
        tx.commit();

    }
    public void removeA(View view){
        fa = (FragmentA) mgr.findFragmentByTag("A");
        if(fa!=null) {
            tx = mgr.beginTransaction();
            tx.remove(fa);
            tx.addToBackStack("RA");
            tx.commit();
        }

    }
    public void replaceAB(View view){
        fb = new FragmentB();
        if(fb!=null) {
            tx = mgr.beginTransaction();
            tx.replace(R.id.group, fb, "B");
            tx.addToBackStack("RPA");
            tx.commit();
        }
    }
    public void addB(View view){
        fb = new FragmentB();
            tx = mgr.beginTransaction();
            tx.add(R.id.group, fb, "B");
            tx.addToBackStack("B");
            tx.commit();
    }
    public void removeB(View view){
        fb = (FragmentB) mgr.findFragmentByTag("B");
        if(fb!=null) {
            tx = mgr.beginTransaction();
            tx.remove(fb);
            tx.addToBackStack("RB");
            tx.commit();
        }
    }
    public void replaceBA(View view){
        fa = new FragmentA();
        if(fa!=null) {
            tx = mgr.beginTransaction();
            tx.replace(R.id.group, fa, "A");
            tx.addToBackStack("RPB");
            tx.commit();
        }
    }
    public void attachA(View view){
        fa = (FragmentA) mgr.findFragmentByTag("A");
        if(fa!=null) {
            tx = mgr.beginTransaction();
            tx.attach(fa);
            tx.addToBackStack("AA");
            tx.commit();
        }
    }
    public void detachA(View view){
        fa  = (FragmentA) mgr.findFragmentByTag("A");
        if(fa!=null) {
            tx = mgr.beginTransaction();
            tx.detach(fa);
            tx.addToBackStack("DA");
            tx.commit();
        }
    }
    public void back(View view){
        mgr.popBackStack();
    }
    public void pop(View view){

    }
}

我使用的是Android Studio 3.0.1。有人能告诉我它有什么问题吗?

android fragment
1个回答
0
投票

你正在使用AppCompatActivity,所以你需要使用getSupportFragmentManager()而不是getFragmentManager(),请参阅doc:https://developer.android.com/reference/android/support/v4/app/FragmentActivity.html

© www.soinside.com 2019 - 2024. All rights reserved.