Android 片段弹出堆栈

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25932761/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:51:33  来源:igfitidea点击:

Fragment PopBackStack

androidandroid-fragmentsback-stackonbackpressed

提问by Gaurav Arora

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

在使用 Fragments 并将它们弹出时,我遇到了一个奇怪的问题。

I have one Fragment Activity:

我有一个片段活动:

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

第 1 步:我在名为 Fragment A 的开始中将该活动的 onCreate 中的一个 Fragment 附加为:

This is the onCreate of Fragment Activity

这是 Fragment Activity 的 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();
    }

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

步骤:2 之后,我附上了用以下代码替换了 Fragment:

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

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

第 3 步:在此之后,我再次以相同的方式在 Fragment_More 上添加了一个 Fragment:

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

and here is my Fragment's Activity onBackPressedButton Lisetener:

这是我的 Fragment 活动 onBackPressedButton Lisetener:

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

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

Here is the Problem which I am getting:

这是我遇到的问题:

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.

当我从第 3 步按回时,它会按照 onBackPressed 的描述成功地为我提供上一个片段,它将 popbackstack 并将我带到上一个片段。但是从 Fragment_More 中,当我再次按回时,它显示我空白的 FRAGMENT 内容。它从不显示我在活动的 OnCreate 的步骤 1 中添加的第一个片段。

Please comment. What I am doing wrong ?

请给出意见。我做错了什么?

Thanks

谢谢

回答by Chris

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!

您不应该将您的第一个片段添加到 backstack,因为按返回键只会反转您所做的操作。因此,如果您替换一个片段并按回,它将反转您所做的替换。但是,如果您按回一个 ' .add()' 片段事务,它会简单地将其删除并显示一个空白页面。所以我认为从 backstack 中删除第一个片段应该可以解决问题。试试看,让我知道!

@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();
    }

EXAMPLE:

例子:

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

所以假设你有两个片段叫做A& B

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

如果您将 A.add()片段添加到页面,并且“ ”(= 保存操作的反向),则当您单击返回时,您将已将 REMOVE 片段A保存为操作。addToBackStack()

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

如果.replace()片段一个通过片段,也“ addToBackStack()”你会保存在删除,并添加一个,当您点击后面的动作。

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

所以你明白为什么将你的第一个 fragmenttransaction 添加到 backstack 不是一个好习惯吗?它只会导致通过按回删除片段A,从而导致空白页面。希望这是足够的信息。

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.

此外,一旦您将 删除addToBackStack()到您的第一个片段,您就不需要覆盖,onBackPressed()因为它将按照预期的方式工作。当 backstack 中没有任何东西时,它只会完成活动,而不会先向您显示该空白页。

回答by Jerry Frost

Your line:

您的线路:

if(fm.getBackStackEntryCount() > 1){

should be:

应该:

if (fm.getBackStackEntryCount() > 0) {

You are putting 3 fragments on the backstack. After you pop the first two entries off, the count of entries on the backstack then equals 1. So you prevent yourself from retrieving the first fragment you added to the backstack.

您将 3 个片段放在 backstack 上。在弹出前两个条目后,backstack 上的条目计数等于 1。因此您防止自己检索添加到 backstack 的第一个片段。

回答by mujeeb.omr

You are using this

你正在使用这个

manager.beginTransaction().add(R.id.frameContent, home).addToBackStack("home").commit();

try to use this instead

尝试使用它代替

manager.beginTransaction().replace(R.id.frameContent, home).addToBackStack("home").commit();

Let me know if it works for you.

请让我知道这对你有没有用。