Android的onBackStackChanged

Android的onBackStackChanged

本文介绍了Android的onBackStackChanged()不叫的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个活动像code如下:

I have an Activity like the code below:

public class AuthenticatedMainActivity extends Activity implements FragmentManager.OnBackStackChangedListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        .............
        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction().add(R.id.frame_container,fragment, "First").addToBackStack(null).commit();
    }

    private void switchFragment(Fragment fragment){
      FragmentManager fragmentManager = getFragmentManager();
      fragmentManager.beginTransaction()
        .replace(R.id.frame_container, fragment).addToBackStack("Tag").commit();
    }

    @Override
public void onBackStackChanged() {
    System.out.println("====================================================changeeeeeeeeeeeeeeeeeeeeeeeeee");
}
}

onBackStackChanged()方法不会被调用甚至与 switchFragment 方法后成功取代片段。

But the onBackStackChanged() method is never called even After successfully replaced fragments with the switchFragment method.

推荐答案

您需要添加活动 OnBackStackChangedListener 使用 addOnBackStackChangedListener

例如:

@Override
protected void onCreate(Bundle savedInstanceState) {
    .............
    FragmentManager fragmentManager = getFragmentManager();
    fragmentManager.addOnBackStackChangedListener(this);
    fragmentManager.beginTransaction().add(R.id.frame_container,fragment, "First").addToBackStack(null).commit();
}

这篇关于Android的onBackStackChanged()不叫的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 16:08