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

时间:2018-02-28 08:48:57

标签: android fragment

我是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 。有人能告诉我它有什么问题吗?

2 个答案:

答案 0 :(得分:1)

addOnBackStackChangedListener用于在Android 3.0之前的平台上运行的应用程序。在Android 3.0或更高版本上运行时,仍会使用此实现,但它不会尝试切换到框架的实现,因此不会被调用。

答案 1 :(得分:0)

您正在使用AppCompatActivity,因此您需要使用。{1}}  getSupportFragmentManager()而不是getFragmentManager(),请参阅文档:https://developer.android.com/reference/android/support/v4/app/FragmentActivity.html