安卓4.2:与嵌套回片段堆栈行为 [英] Android 4.2: back stack behaviour with nested fragments

查看:149
本文介绍了安卓4.2:与嵌套回片段堆栈行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用的Andr​​oid 4.2,支持库得到支持嵌套片段看到这里。我研究了一下,结果发现了一个有趣的现象/错误就回堆栈和<一href="https://developer.android.com/reference/android/support/v4/app/Fragment.html#getChildFragmentManager%28%29">getChildFragmentManager().当使用getChildFragmentManager()和addToBackStack(字符串名称),以pressing后退按钮的系统没有办下来回堆到previous片段。 另一方面,当使用<一href="https://developer.android.com/reference/android/support/v4/app/Fragment.html#getFragmentManager%28%29">getFragmentManager()和addToBackStack(字符串名称),以pressing后退按钮,系统返回到previous片段。

对于我来说,这种行为是意外。通过pressing我的设备上的后退按钮,我期待的最后一个片段添加到后面堆栈将被弹出,即使片段加到后面堆在孩子们的片段经理。

时的这种行为是否正确?这种行为是一个错误?有没有一种解决此问题?

样品code与getChildFragmentManager():

 公共类FragmentceptionActivity扩展FragmentActivity {

@覆盖
保护无效的onCreate(捆绑为arg0){
    super.onCreate(为arg0);

    最后的FrameLayout wrapper1 =新的FrameLayout(本);
    wrapper1.setLayoutParams(新FrameLayout.LayoutParams(
            FrameLayout.LayoutParams.MATCH_PARENT,
            FrameLayout.LayoutParams.MATCH_PARENT));
    wrapper1.setId(1);

    最后FrameLayout.LayoutParams PARAMS =新FrameLayout.LayoutParams(
            FrameLayout.LayoutParams.MATCH_PARENT,
            FrameLayout.LayoutParams.WRAP_CONTENT);
    params.topMargin = 0;

    最后的TextView文本=新的TextView(本);
    text.setLayoutParams(PARAMS);
    text.setText(片段1);
    wrapper1.addView(文本);

    的setContentView(wrapper1);

    getSupportFragmentManager()的BeginTransaction()。addToBackStack(空)
            。新增(1,新片段1())提交()。
}

公共类片段1扩展片段{
    @覆盖
    公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
            捆绑savedInstanceState){
        最后的FrameLayout wrapper2 =新的FrameLayout(getActivity());
        wrapper2.setLayoutParams(新FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.MATCH_PARENT,
                FrameLayout.LayoutParams.MATCH_PARENT));
        wrapper2.setId(2);

        最后FrameLayout.LayoutParams PARAMS =新FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.MATCH_PARENT,
                FrameLayout.LayoutParams.WRAP_CONTENT);
        params.topMargin = 100;

        最后的TextView文本=新的TextView(getActivity());
        text.setLayoutParams(PARAMS);
        text.setText(片段2);
        wrapper2.addView(文本);

        返回wrapper2;
    }

    @覆盖
    公共无效onViewCreated(查看视图,捆绑savedInstanceState){
        super.onViewCreated(查看,savedInstanceState);

        getFragmentManager()的BeginTransaction()。addToBackStack(空)
                。新增(2,新Fragment2())提交()。
    }
}

公共类Fragment2扩展片段{
    @覆盖
    公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
            捆绑savedInstanceState){
        最后的FrameLayout wrapper3 =新的FrameLayout(getActivity());
        wrapper3.setLayoutParams(新FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.MATCH_PARENT,
                FrameLayout.LayoutParams.MATCH_PARENT));
        wrapper3.setId(3);

        最后FrameLayout.LayoutParams PARAMS =新FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.MATCH_PARENT,
                FrameLayout.LayoutParams.WRAP_CONTENT);
        params.topMargin = 200;

        最后的TextView文本=新的TextView(getActivity());
        text.setLayoutParams(PARAMS);
        text.setText(片段3);
        wrapper3.addView(文本);

        返回wrapper3;
    }

    @覆盖
    公共无效onViewCreated(查看视图,捆绑savedInstanceState){
        super.onViewCreated(查看,savedInstanceState);

        getChildFragmentManager()的BeginTransaction()。addToBackStack(空)
                。新增(3,新Fragment3())提交()。
    }
}

公共类Fragment3扩展片段{
    @覆盖
    公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
            捆绑savedInstanceState){
        最后的FrameLayout wrapper4 =新的FrameLayout(getActivity());
        wrapper4.setLayoutParams(新FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.MATCH_PARENT,
                FrameLayout.LayoutParams.MATCH_PARENT));
        wrapper4.setId(4);

        最后FrameLayout.LayoutParams PARAMS =新FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.MATCH_PARENT,
                FrameLayout.LayoutParams.WRAP_CONTENT);
        params.topMargin = 300;

        最后的TextView文本=新的TextView(getActivity());
        text.setLayoutParams(PARAMS);
        text.setText(片段4);
        wrapper4.addView(文本);

        返回wrapper4;
    }

    @覆盖
    公共无效onViewCreated(查看视图,捆绑savedInstanceState){
        super.onViewCreated(查看,savedInstanceState);

        getChildFragmentManager()的BeginTransaction()。addToBackStack(空)
                。新增(4,新Fragment4())提交()。
    }
}

公共类Fragment4扩展片段{
    @覆盖
    公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
            捆绑savedInstanceState){
        最后的FrameLayout wrapper5 =新的FrameLayout(getActivity());
        wrapper5.setLayoutParams(新FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.MATCH_PARENT,
                FrameLayout.LayoutParams.MATCH_PARENT));
        wrapper5.setId(5);

        最后FrameLayout.LayoutParams PARAMS =新FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.MATCH_PARENT,
                FrameLayout.LayoutParams.WRAP_CONTENT);
        params.topMargin = 400;

        最后的TextView文本=新的TextView(getActivity());
        text.setLayoutParams(PARAMS);
        text.setText(片段5);
        wrapper5.addView(文本);

        返回wrapper5;
    }

    @覆盖
    公共无效onViewCreated(查看视图,捆绑savedInstanceState){
        super.onViewCreated(查看,savedInstanceState);
    }
}

}
 

样品code与getFragmentManager():

 公共类FragmentceptionActivity扩展FragmentActivity {

@覆盖
保护无效的onCreate(捆绑为arg0){
    super.onCreate(为arg0);

    最后的FrameLayout wrapper1 =新的FrameLayout(本);
    wrapper1.setLayoutParams(新FrameLayout.LayoutParams(
            FrameLayout.LayoutParams.MATCH_PARENT,
            FrameLayout.LayoutParams.MATCH_PARENT));
    wrapper1.setId(1);

    最后FrameLayout.LayoutParams PARAMS =新FrameLayout.LayoutParams(
            FrameLayout.LayoutParams.MATCH_PARENT,
            FrameLayout.LayoutParams.WRAP_CONTENT);
    params.topMargin = 0;

    最后的TextView文本=新的TextView(本);
    text.setLayoutParams(PARAMS);
    text.setText(片段1);
    wrapper1.addView(文本);

    的setContentView(wrapper1);

    getSupportFragmentManager()的BeginTransaction()。addToBackStack(空)
            。新增(1,新片段1())提交()。
}

公共类片段1扩展片段{
    @覆盖
    公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
            捆绑savedInstanceState){
        最后的FrameLayout wrapper2 =新的FrameLayout(getActivity());
        wrapper2.setLayoutParams(新FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.MATCH_PARENT,
                FrameLayout.LayoutParams.MATCH_PARENT));
        wrapper2.setId(2);

        最后FrameLayout.LayoutParams PARAMS =新FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.MATCH_PARENT,
                FrameLayout.LayoutParams.WRAP_CONTENT);
        params.topMargin = 100;

        最后的TextView文本=新的TextView(getActivity());
        text.setLayoutParams(PARAMS);
        text.setText(片段2);
        wrapper2.addView(文本);

        返回wrapper2;
    }

    @覆盖
    公共无效onViewCreated(查看视图,捆绑savedInstanceState){
        super.onViewCreated(查看,savedInstanceState);

        getFragmentManager()的BeginTransaction()。addToBackStack(空)
                。新增(2,新Fragment2())提交()。
    }
}

公共类Fragment2扩展片段{
    @覆盖
    公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
            捆绑savedInstanceState){
        最后的FrameLayout wrapper3 =新的FrameLayout(getActivity());
        wrapper3.setLayoutParams(新FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.MATCH_PARENT,
                FrameLayout.LayoutParams.MATCH_PARENT));
        wrapper3.setId(3);

        最后FrameLayout.LayoutParams PARAMS =新FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.MATCH_PARENT,
                FrameLayout.LayoutParams.WRAP_CONTENT);
        params.topMargin = 200;

        最后的TextView文本=新的TextView(getActivity());
        text.setLayoutParams(PARAMS);
        text.setText(片段3);
        wrapper3.addView(文本);

        返回wrapper3;
    }

    @覆盖
    公共无效onViewCreated(查看视图,捆绑savedInstanceState){
        super.onViewCreated(查看,savedInstanceState);

        getFragmentManager()的BeginTransaction()。addToBackStack(空)
                。新增(3,新Fragment3())提交()。
    }
}

公共类Fragment3扩展片段{
    @覆盖
    公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
            捆绑savedInstanceState){
        最后的FrameLayout wrapper4 =新的FrameLayout(getActivity());
        wrapper4.setLayoutParams(新FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.MATCH_PARENT,
                FrameLayout.LayoutParams.MATCH_PARENT));
        wrapper4.setId(4);

        最后FrameLayout.LayoutParams PARAMS =新FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.MATCH_PARENT,
                FrameLayout.LayoutParams.WRAP_CONTENT);
        params.topMargin = 300;

        最后的TextView文本=新的TextView(getActivity());
        text.setLayoutParams(PARAMS);
        text.setText(片段4);
        wrapper4.addView(文本);

        返回wrapper4;
    }

    @覆盖
    公共无效onViewCreated(查看视图,捆绑savedInstanceState){
        super.onViewCreated(查看,savedInstanceState);

        getFragmentManager()的BeginTransaction()。addToBackStack(空)
                。新增(4,新Fragment4())提交()。
    }
}

公共类Fragment4扩展片段{
    @覆盖
    公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
            捆绑savedInstanceState){
        最后的FrameLayout wrapper5 =新的FrameLayout(getActivity());
        wrapper5.setLayoutParams(新FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.MATCH_PARENT,
                FrameLayout.LayoutParams.MATCH_PARENT));
        wrapper5.setId(5);

        最后FrameLayout.LayoutParams PARAMS =新FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.MATCH_PARENT,
                FrameLayout.LayoutParams.WRAP_CONTENT);
        params.topMargin = 400;

        最后的TextView文本=新的TextView(getActivity());
        text.setLayoutParams(PARAMS);
        text.setText(片段5);
        wrapper5.addView(文本);

        返回wrapper5;
    }

    @覆盖
    公共无效onViewCreated(查看视图,捆绑savedInstanceState){
        super.onViewCreated(查看,savedInstanceState);
    }
}

}
 

解决方案

似乎是一个错误。 看一眼: <一href="http://$c$c.google.com/p/android/issues/detail?id=40323">http://$c$c.google.com/p/android/issues/detail?id=40323

有关一种变通方法我已经成功地使用(如在评论建议):

  @覆盖
公共无效onBack pressed(){

    //如果该片段存在,并且具有一定的背堆栈条目
    如果(mActivityDirectFragment = NULL和放大器;!&安培; mActivityDirectFragment.getChildFragmentManager()getBackStackEntryCount()&GT; 0){
        //获取片段片段经理 - 并弹出backstack
        mActivityDirectFragment.getChildFragmentManager()popBackStack()。
    }
    //否则,没有什么直接的片段回栈
    其他{
        //让超级处理后面的preSS
        super.onBack pressed();
    }
}
 

With Android 4.2, the support library got support for nested fragments see here. I've played around with it and found an interesting behaviour / bug regarding back stack and getChildFragmentManager(). When using getChildFragmentManager() and addToBackStack(String name), by pressing the back button the system does not run down the back stack to the previous fragment. On the other hand, when using getFragmentManager() and addToBackStack(String name), by pressing the back button the system returns to the previous fragment.

For me, this behaviour is unexpected. By pressing the back button on my device, I'm expecting that the last added fragment to the back stack will be popped, even if the fragment was added to the back stack in the children's fragment manager.

Is this behaviour correct? Is this behaviour a bug? Is there a work around for this issue?

sample code with getChildFragmentManager():

public class FragmentceptionActivity extends FragmentActivity {

@Override
protected void onCreate(Bundle arg0) {
    super.onCreate(arg0);

    final FrameLayout wrapper1 = new FrameLayout(this);
    wrapper1.setLayoutParams(new FrameLayout.LayoutParams(
            FrameLayout.LayoutParams.MATCH_PARENT,
            FrameLayout.LayoutParams.MATCH_PARENT));
    wrapper1.setId(1);

    final FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
            FrameLayout.LayoutParams.MATCH_PARENT,
            FrameLayout.LayoutParams.WRAP_CONTENT);
    params.topMargin = 0;

    final TextView text = new TextView(this);
    text.setLayoutParams(params);
    text.setText("fragment 1");
    wrapper1.addView(text);

    setContentView(wrapper1);

    getSupportFragmentManager().beginTransaction().addToBackStack(null)
            .add(1, new Fragment1()).commit();
}

public class Fragment1 extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        final FrameLayout wrapper2 = new FrameLayout(getActivity());
        wrapper2.setLayoutParams(new FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.MATCH_PARENT,
                FrameLayout.LayoutParams.MATCH_PARENT));
        wrapper2.setId(2);

        final FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.MATCH_PARENT,
                FrameLayout.LayoutParams.WRAP_CONTENT);
        params.topMargin = 100;

        final TextView text = new TextView(getActivity());
        text.setLayoutParams(params);
        text.setText("fragment 2");
        wrapper2.addView(text);

        return wrapper2;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        getFragmentManager().beginTransaction().addToBackStack(null)
                .add(2, new Fragment2()).commit();
    }
}

public class Fragment2 extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        final FrameLayout wrapper3 = new FrameLayout(getActivity());
        wrapper3.setLayoutParams(new FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.MATCH_PARENT,
                FrameLayout.LayoutParams.MATCH_PARENT));
        wrapper3.setId(3);

        final FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.MATCH_PARENT,
                FrameLayout.LayoutParams.WRAP_CONTENT);
        params.topMargin = 200;

        final TextView text = new TextView(getActivity());
        text.setLayoutParams(params);
        text.setText("fragment 3");
        wrapper3.addView(text);

        return wrapper3;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        getChildFragmentManager().beginTransaction().addToBackStack(null)
                .add(3, new Fragment3()).commit();
    }
}

public class Fragment3 extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        final FrameLayout wrapper4 = new FrameLayout(getActivity());
        wrapper4.setLayoutParams(new FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.MATCH_PARENT,
                FrameLayout.LayoutParams.MATCH_PARENT));
        wrapper4.setId(4);

        final FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.MATCH_PARENT,
                FrameLayout.LayoutParams.WRAP_CONTENT);
        params.topMargin = 300;

        final TextView text = new TextView(getActivity());
        text.setLayoutParams(params);
        text.setText("fragment 4");
        wrapper4.addView(text);

        return wrapper4;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        getChildFragmentManager().beginTransaction().addToBackStack(null)
                .add(4, new Fragment4()).commit();
    }
}

public class Fragment4 extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        final FrameLayout wrapper5 = new FrameLayout(getActivity());
        wrapper5.setLayoutParams(new FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.MATCH_PARENT,
                FrameLayout.LayoutParams.MATCH_PARENT));
        wrapper5.setId(5);

        final FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.MATCH_PARENT,
                FrameLayout.LayoutParams.WRAP_CONTENT);
        params.topMargin = 400;

        final TextView text = new TextView(getActivity());
        text.setLayoutParams(params);
        text.setText("fragment 5");
        wrapper5.addView(text);

        return wrapper5;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }
}

}

sample code with getFragmentManager():

public class FragmentceptionActivity extends FragmentActivity {

@Override
protected void onCreate(Bundle arg0) {
    super.onCreate(arg0);

    final FrameLayout wrapper1 = new FrameLayout(this);
    wrapper1.setLayoutParams(new FrameLayout.LayoutParams(
            FrameLayout.LayoutParams.MATCH_PARENT,
            FrameLayout.LayoutParams.MATCH_PARENT));
    wrapper1.setId(1);

    final FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
            FrameLayout.LayoutParams.MATCH_PARENT,
            FrameLayout.LayoutParams.WRAP_CONTENT);
    params.topMargin = 0;

    final TextView text = new TextView(this);
    text.setLayoutParams(params);
    text.setText("fragment 1");
    wrapper1.addView(text);

    setContentView(wrapper1);

    getSupportFragmentManager().beginTransaction().addToBackStack(null)
            .add(1, new Fragment1()).commit();
}

public class Fragment1 extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        final FrameLayout wrapper2 = new FrameLayout(getActivity());
        wrapper2.setLayoutParams(new FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.MATCH_PARENT,
                FrameLayout.LayoutParams.MATCH_PARENT));
        wrapper2.setId(2);

        final FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.MATCH_PARENT,
                FrameLayout.LayoutParams.WRAP_CONTENT);
        params.topMargin = 100;

        final TextView text = new TextView(getActivity());
        text.setLayoutParams(params);
        text.setText("fragment 2");
        wrapper2.addView(text);

        return wrapper2;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        getFragmentManager().beginTransaction().addToBackStack(null)
                .add(2, new Fragment2()).commit();
    }
}

public class Fragment2 extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        final FrameLayout wrapper3 = new FrameLayout(getActivity());
        wrapper3.setLayoutParams(new FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.MATCH_PARENT,
                FrameLayout.LayoutParams.MATCH_PARENT));
        wrapper3.setId(3);

        final FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.MATCH_PARENT,
                FrameLayout.LayoutParams.WRAP_CONTENT);
        params.topMargin = 200;

        final TextView text = new TextView(getActivity());
        text.setLayoutParams(params);
        text.setText("fragment 3");
        wrapper3.addView(text);

        return wrapper3;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        getFragmentManager().beginTransaction().addToBackStack(null)
                .add(3, new Fragment3()).commit();
    }
}

public class Fragment3 extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        final FrameLayout wrapper4 = new FrameLayout(getActivity());
        wrapper4.setLayoutParams(new FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.MATCH_PARENT,
                FrameLayout.LayoutParams.MATCH_PARENT));
        wrapper4.setId(4);

        final FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.MATCH_PARENT,
                FrameLayout.LayoutParams.WRAP_CONTENT);
        params.topMargin = 300;

        final TextView text = new TextView(getActivity());
        text.setLayoutParams(params);
        text.setText("fragment 4");
        wrapper4.addView(text);

        return wrapper4;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        getFragmentManager().beginTransaction().addToBackStack(null)
                .add(4, new Fragment4()).commit();
    }
}

public class Fragment4 extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        final FrameLayout wrapper5 = new FrameLayout(getActivity());
        wrapper5.setLayoutParams(new FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.MATCH_PARENT,
                FrameLayout.LayoutParams.MATCH_PARENT));
        wrapper5.setId(5);

        final FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.MATCH_PARENT,
                FrameLayout.LayoutParams.WRAP_CONTENT);
        params.topMargin = 400;

        final TextView text = new TextView(getActivity());
        text.setLayoutParams(params);
        text.setText("fragment 5");
        wrapper5.addView(text);

        return wrapper5;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }
}

}

解决方案

Seems like a bug. Take a look at: http://code.google.com/p/android/issues/detail?id=40323

For a workaround I've used successfully (as suggested in comments):

    @Override
public void onBackPressed() {

    // If the fragment exists and has some back-stack entry
    if (mActivityDirectFragment != null && mActivityDirectFragment.getChildFragmentManager().getBackStackEntryCount() > 0){
        // Get the fragment fragment manager - and pop the backstack
        mActivityDirectFragment.getChildFragmentManager().popBackStack();
    }
    // Else, nothing in the direct fragment back stack
    else{
        // Let super handle the back press
        super.onBackPressed();          
    }
}

这篇关于安卓4.2:与嵌套回片段堆栈行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆