向片段添加片段时,后退按钮不起作用 [英] Back button not working when adding fragment to backstack

查看:76
本文介绍了向片段添加片段时,后退按钮不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图弄清楚这一点.

I've been trying to figure this one out.

我首先要说有很多StackOverflow解决方案,但是大多数人说我自己连接onBackPressed()确实可行,但我不明白为什么我不明白这一点..addToBackStack

I'll start by saying that there are many StackOverflow solutions but most say to hookup the onBackPressed() myself, which does work, but I don't understand why I don't get that behavior for free with the .addToBackStack

在文档中找不到任何相关内容,除非它应该可以工作.

Can't find anything relevant in the documentation except that it should have worked.

我正在使用最简单的形式向后堆栈添加一个片段

I am using the simplest of forms to add a fragment to the backstack

getActivity().getSupportFragmentManager().beginTransaction().add(R.id.create_fragment_holder2, new MyFragment(), TAG).addToBackStack(TAG).commit();

getActivity是一个FragmentActivity.与此FrameLayout一起使用:

getActivity is a FragmentActivity. Goes along with this FrameLayout:

 <FrameLayout
        android:id="@+id/create_card_fragment_holder2"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

还有一个带有蓝色背景的TextView的简单片段

And a simple fragment with a TextView with a blue background

public class MyFragment extends Fragment{

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
        @Nullable Bundle savedInstanceState) {
    return inflater.inflate(R.layout.my_layout, container, false);
}}

问题是,如果我不询问.addToBackStack,那么后退按钮将关闭包含的片段,如果我这样做,则后退按钮无响应,并且我将永远看到该蓝屏.

Problem is that if I don't ask .addToBackStack then the back button will close the containing fragment, and if I do, the back button is non responsive and I will forever see that blue screen.

我发现的每个StackOverflow解决方案都说要我自己连接onBackPressed(),这确实有效,但是我不明白为什么我不能通过.addToBackStack来免费获得这种行为在文档中找不到任何相关内容,但应该可以使用.

Every StackOverflow solution I found says to hookup the onBackPressed() myself, which does work, but I don't understand why I don't get that behavior for free with the .addToBackStack Can't find anything relevant in the documentation except that it should have worked.

更新:我发现它不起作用,因为我在活动的onBackPress中阻止了它.因此,如果没有它,它将按预期工作.也就是说,如果需要的话,这是输入验证并阻止验证起作用的好地方:)

UPDATE: I found out it was not working because I was blocking it in the onBackPress in the activity. So without it it will work as expected. That said it's a good place to enter validation and prevent it from working, should that be your need :)

推荐答案

onBackPressed()覆盖到您的活动中,并进行调用,以便从后堆栈中删除当前片段,因为您将其添加了.>

Override onBackPressed() into your activity and call this in order to remove current fragment from backstack, since you add it.

if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
    getSupportFragmentManager().popBackStackImmediate()
} else {
    finish();
}

仅当您使用 addToBackStack()方法将片段添加到后向堆栈时,此方法才有效.

This will work only when you add fragment to backstack using addToBackStack() method.

当您向后向堆栈中添加片段以继续跟踪回流和所有先前的更改时,该实例将保留在 FragmentManager 中.当您想返回上一个片段时,只需从堆栈中弹出最新的片段即可.如果不将其添加到堆栈中,将无法回滚采用的路径和所有先前的操作.

When you add a fragment to backstack to keep tracking your back flow and all the previous changes, that instance will be keeped into FragmentManager. When you want to go back to previous fragment, just pop the latest fragment from backstack. If you don't add it to stack, you will not be able to roll back the taken path and all the previous oprations.

这篇关于向片段添加片段时,后退按钮不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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