片段PopBackStack [英] Fragment PopBackStack

查看:78
本文介绍了片段PopBackStack的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用片段并将其弹出后,我遇到了一个奇怪的问题.

I am getting a strange problem, while using Fragments and popping back them out.

我有一个片段活动:

步骤1:我在名为Fragment A的开始"中的该活动的onCreate上附加一个片段,如下所示:

Step1: I am attaching one Fragment in the onCreate of that Activity in the Starting named Fragment A as:

这是片段活动的onCreate

@Override
    protected void onCreate(Bundle savedBundleState) {
        super.onCreate(savedBundleState);
        setContentView(R.layout.activity_base_new);

        Fragement_Home home = new Fragement_Home();
        FragmentManager manager = getSupportFragmentManager();
        manager.beginTransaction().add(R.id.frameContent, home).addToBackStack("home").commit();
    }

步骤:2之后,我用以下代码替换了片段:

Step:2 After that I attached replaced the Fragment with the following code:

Fragment_More moreFragment = new Fragment_More();
            FragmentManager manager = getSupportFragmentManager();
            manager.beginTransaction().replace(R.id.frameContent, moreFragment).addToBackStack("more").commit();

步骤3:之后,我又以相同的方式在Fragment_More上添加了一个片段:

Step 3: After that again I added one more Fragment on Fragment_More in the same way:

Fragment_AboutRockstand termsConditions = new Fragment_AboutRockstand();
            getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.frameContent, termsConditions).addToBackStack("about_rockstand").commit();

这是onBackPressedButton Lisetener上我片段的活动:

and here is my Fragment's Activity onBackPressedButton Lisetener:

@Override
public void onBackPressed() {
    FragmentManager fm = getSupportFragmentManager();
    Fragment f = fm.findFragmentById(R.id.frameContent);

 if(fm.getBackStackEntryCount() > 1){
         fm.popBackStack();
    } else{
        finish();
    }
}

这是我遇到的问题:

当我从第3步按回时,如onBackPressed所述,它成功地给了我上一个片段,它将弹出backstack,并将我带到上一个片段.但是从Fragment_More中,当我再次按回时,它显示了空白的FRAGMENT内容.它从不显示我在活动的OnCreate的第1步中添加的第一个片段.

When I press back from Step 3, it gives me previous fragment successfully as described by onBackPressed, it will popbackstack and will take me to the previous Fragment. But from the Fragment_More, when I press back again, it shows me blank FRAGMENT content. It never shows the First Fragment which I had added on Step 1 in the OnCreate of the Activity.

请发表评论.我做错了什么?

Please comment. What I am doing wrong ?

谢谢

推荐答案

您不应将第一个片段添加到Backstack中,因为按回车键只会使您执行的操作相反.因此,如果您替换一个片段并按回,它将使您所做的替换相反.但是,如果您按下" .add()" fragmenttransaction,它将简单地将其删除并显示空白页.因此,我认为从后堆栈中删除第一个片段应该可以解决问题.试试看,让我知道!

You should not add your first fragment to the backstack, as pressing the return key will just reverse the action you did. So if you replace a fragment and you press back, it will reverse the replace you did. But if you press back on an '.add()' fragmenttransaction, it will simply remove it and show you a blank page. So i think removing the first fragment from the backstack should do the trick. Try it out and let me know!

@Override
    protected void onCreate(Bundle savedBundleState) {
        super.onCreate(savedBundleState);
        setContentView(R.layout.activity_base_new);

        Fragement_Home home = new Fragement_Home();
        FragmentManager manager = getSupportFragmentManager();
        manager.beginTransaction().add(R.id.frameContent, home).commit();
    }

示例:

因此,假设您有两个名为 A 的片段, B

So let's say you have two fragments called A & B

如果您在页面中 A 片段 .add()和" addToBackStack()"(=保存操作的反向操作),当您单击返回时,您会将REMOVE片段 A 保存为操作.

If you .add() fragment A to the page, and "addToBackStack()" ( = save reverse of the action ) you will have saved the REMOVE fragment A as action when you click on back.

如果您 .replace()片段 A 和片段 B 以及" addToBackStack()"您将保存删除的 B 并在单击后添加 A 作为操作.

If you .replace() fragment A by fragment B, and also "addToBackStack()" you will have saved the remove B and add A as action when you click on back.

所以您明白了为什么将第一个fragmenttransaction添加到backstack不是一个好习惯吗?只需按回去即可删除片段 A ,从而导致空白页.希望这是足够的信息.

So you see why it's not good practice to add your first fragmenttransaction to the backstack? It will just result in removing fragment A by pressing back, resulting in a blank page. Hope this is enough info.

此外,将 addToBackStack()删除到第一个片段后,您无需覆盖 onBackPressed(),因为它将按预期工作到.如果没有在堆栈中留下任何内容而没有先显示空白页,它将结束活动.

Also, once you have removed the addToBackStack() to your first fragment, you don't need to override onBackPressed() because it will work how it is supposed to. It will just finish the activity when there is nothing left in the backstack without showing you that blank page first.

这篇关于片段PopBackStack的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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