简体   繁体   English

向片段添加片段时,后退按钮不起作用

[英]Back button not working when adding fragment to backstack

I've been trying to figure this one out. 我一直在努力找出这一点。

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 首先,我说有很多StackOverflow解决方案,但是大多数人说我自己连接onBackPressed() 确实有效,但是我不明白为什么我不能通过.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 is a FragmentActivity. getActivity是一个FragmentActivity。 Goes along with this FrameLayout: 与此FrameLayout一起使用:

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

And a simple fragment with a TextView with a blue background 还有一个带有蓝色背景的TextView的简单片段

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

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. 问题是,如果我不询问.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. 我发现的每个StackOverflow解决方案都说我自己连接onBackPressed()确实有效,但是我不明白为什么我不能免费使用.addToBackStack来获得这种行为。在文档中找不到任何相关的内容,除了它应该工作了。

UPDATE : I found out it was not working because I was blocking it in the onBackPress in the activity. 更新 :我发现它不起作用,因为我在活动的onBackPress中阻止了它。 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 :) 就是说,如果需要的话,这是输入验证并阻止验证起作用的好地方:)

Override onBackPressed() into your activity and call this in order to remove current fragment from backstack, since you add it. onBackPressed()覆盖到您的活动中并调用它,以便从后堆栈中删除当前片段,因为您已添加了它。

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

This will work only when you add fragment to backstack using addToBackStack() method. 仅当您使用addToBackStack()方法将片段添加到后堆栈时,此方法才有效。

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 . 当您将片段添加到backstack中以继续跟踪您的回流和所有先前的更改时,该实例将保留在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 looks like you know how to get around the issue you're running into, and your post seems to be asking one specific question, so I'll answer that: 看来您知道如何解决您遇到的问题,而您的帖子似乎在问一个特定的问题,因此我将回答:

I don't understand why I don't get that behavior for free with the .addToBackStack 我不明白为什么我不能通过.addToBackStack来免费获得这种行为

It does! 是的! But only to a point. 但是只有一点。 The back button will automatically navigate back through the back stack, but it will not close the last fragment, if there is nothing to navigate back to. “后退”按钮将自动在后退堆栈中导航,但是如果没有导航到的内容,它将不会关闭最后一个片段。 So, if you were to add another fragment to the back stack (and not override the back button), it would automatically navigate the user back to your first fragment, but then do nothing after that. 因此,如果您要向后堆栈添加另一个片段(而不是覆盖后退按钮),它将自动将用户导航回您的第一个片段,但此后不执行任何操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM