简体   繁体   English

按下按钮如何返回上一个片段

[英]How to go back to the previous Fragment on Press of a button

I have 2 fragments, one is added to the backstack, while the other one is not added. 我有2个片段,一个片段被添加到了堆栈中,而另一个片段没有被添加。 Consider that I'm in the 2nd fragment, which is not added to the backstack. 考虑到我在第二个片段中,该片段未添加到后台。 There is a close button on top. 顶部有一个关闭按钮。 On click of this button, I should go back to the previous fragment. 单击此按钮后,我应该返回上一个片段。 How to achieve this? 如何实现呢? I tried something like this: But this can be done, only if its added to the back stack. 我尝试过这样的事情:但是,只有将其添加到后堆栈中,才能做到这一点。

public void onCloseBtnClicked(Fragment fragment, FragmentActivity activity) {
    if(fragment instanceof ImageFragment)
        FragmentHelper.removeCurrentFragment(activity, fragment);
}

public static void removeCurrentFragment(final FragmentActivity activity, final Fragment fragment) {
    if (fragment != null && !fragment.isDetached()) {
        FragmentTransaction transaction = activity.getSupportFragmentManager().beginTransaction();
        transaction.remove(fragment);
        transaction.commitAllowingStateLoss();
    }
}

This is not working, because I didn't add it to the backstack. 这是行不通的,因为我没有将其添加到堆栈中。 Are there any methods to achieve this? 有什么方法可以做到这一点?

Try getFragmentManager().popBackStack() 尝试getFragmentManager().popBackStack()

Also take a look at http://developer.android.com/reference/android/app/FragmentManager.html#popBackStack() 还要看看http://developer.android.com/reference/android/app/FragmentManager.html#popBackStack()

If you use SupportFragment try getSupportFragmentManager().popBackStack(); 如果使用SupportFragment尝试getSupportFragmentManager().popBackStack();

使用getFragmentManager().popBackStack()

Before you commit the transaction with a new fragment, add it to the backstack: 在使用新片段提交事务之前,请将其添加到后台:

transaction.addToBackStack(tag);

So when you wanna go back just: 因此,当您想返回时:

if (mFragmentManager.getBackStackEntryCount() > 1) {
        mFragmentManager.popBackStack();
    }

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

相关问题 按“后退”按钮时片段无法返回到先前的状态 - fragment can not go back to previous state when press Back button 当按返回按钮不更新FragmentAdapter时转到上一个片段 - Go to previous fragment when press back button doesnt update FragmentAdapter 将一个片段替换为另一个片段,返回到工具栏上的上一个片段 - Replace one fragment with another, go back to previous fragment on toolbar back button press 如果在WebView片段中按下“后退”按钮,如何返回上一页? - How to go back to previous page if back button is pressed in WebView fragment? 按下硬件后退按钮时如何返回上一个片段 - how to go back to previous fragment when hardware back button is pressed 需要按两次返回上一个片段 - Need to press back twice to go back to previous fragment 如何在android中点击手机的后退按钮去上一个片段? - How to go the previous fragment on the click of the back button of phone in android? 如何通过操作栏中的后退按钮 go 到上一个片段? (科特林) - How to go to the previous fragment by back button in action bar? (Kotlin) 按物理后退按钮转到上一个片段 - Go to previous fragment pressing physical back button 按下返回时的Android底部导航栏转到上一个片段 - Android bottom navigation bar when press back go to previous fragment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM