按下后退按钮时,如何用片段A替换片段C? [英] How to replace fragment C with fragment A when back button is pressed?

查看:56
本文介绍了按下后退按钮时,如何用片段A替换片段C?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的场景:活动1由片段A-> B-> C组成.所有片段都使用以下代码添加:

My scenario : Activity 1 consists of Fragments A-> B-> C. All the fragments are added using this code :

        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.content, fragment, TAG);
        ft.addToBackStack(TAG);
        ft.commit();

现在,从片段C开始,我想直接返回到片段A.因此,在添加片段C时,我已经注释了 ft.addToBackStack(TAG).因此,当我从CI按返回按钮时直接在屏幕上获取片段A.

Now, from fragment C, I want to directly return to Fragment A. Therefore, I've commented ft.addToBackStack(TAG) while adding Fragment C. So when I press back button from C I directly get Fragment A on the screen.

但是,片段C不会被A代替.实际上,两个片段都是可见的.我该如何解决这个问题?

However, Fragment C is not replaced by A. In fact, both the fragments are visible. How do I solve this issue?

推荐答案

理论

使用 FragmentTransaction 中添加addToBackStack(tag:String):FragmentTransaction 方法,以标记要返回的点.此方法仅返回 FragmentTransaction 实例以实现链式功能.

Use the addToBackStack(tag: String): FragmentTransaction method from within the FragmentTransaction in order to mark a point where you want to return to. This method returns the FragmentTransaction instance for chain-ability only.

然后返回

Then Return with the popBackStackImmediate(tag: String, flag: int): void method from the FragmentManager. The tag is what you specified before. The flag is either the constant POP_BACK_STACK_INCLUSIVE to include the transaction marked or 0.

示例

下面是一个具有以下布局的示例,该布局具有一个ID为 content_frame FrameLayout ,将片段加载到其中.

What follows is an example with the following layout having a FrameLayout with id content_frame where the fragments are loaded into.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />

<FrameLayout
    android:id="@+id/content_frame"
    android:layout_below="@id/textView"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</RelativeLayout>

下面的代码在将布局元素的内容替换为ID content_frame 时,通过其片段类名称标记了一个片段.

The code below marks a fragment by it's fragment class name when replacing the content of the layout element with id content_frame.

public void loadFragment(final Fragment fragment) {
    
    // create a transaction for transition here
    final FragmentTransaction transaction = getSupportFragmentManager()
            .beginTransaction();
            
    // put the fragment in place
    transaction.replace(R.id.content_frame, fragment);

    // this is the part that will cause a fragment to be added to backstack,
    // this way we can return to it at any time using this tag
    transaction.addToBackStack(fragment.getClass().getName());

    transaction.commit();
}

为完成本示例,提供了一种方法,该方法允许您在加载标签时使用标记返回到该相同片段.

And to complete this example a method that allows you to get back to that exact same fragment using the tag when you loaded it.

public void backToFragment(final Fragment fragment) {

    // go back to something that was added to the backstack
    getSupportFragmentManager().popBackStackImmediate(
            fragment.getClass().getName(), 0);
    // use 0 or the below constant as flag parameter
    // FragmentManager.POP_BACK_STACK_INCLUSIVE);

}

真正实现此功能时,您可能需要在片段参数;-)上添加一个空检查.

这篇关于按下后退按钮时,如何用片段A替换片段C?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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