【问题标题】:Loading a Fragment from an Activity to another Activity which is already in backstack将 Activity 中的 Fragment 加载到已经在 backstack 中的另一个 Activity
【发布时间】:2017-11-13 08:38:17
【问题描述】:

我有一个带有底部导航视图的活动(我们称之为 Home Activity),我在其中替换了所有片段。在 Home Activity 中,我加载了一个带有搜索图标的片段。该搜索图标会重定向到 Search Activity。

当我点击任何搜索结果时,我会转到我的主页活动并替换之前的片段以显示我最近的搜索结果点击实现。

在这种情况下,复杂性在于 - 当我尝试替换前一个片段时 - 它对用户可见。因为我有一个过渡动画 - 我可以看到前一个片段正在退出并且最新的片段正在进入。

这对用户来说根本不是一个好的体验。如何改进功能,使用户看不到以前的片段?

【问题讨论】:

    标签: android android-fragments android-activity android-transitions fragment-backstack


    【解决方案1】:

    试试这个代码部分。根据需要创建动画。

    public static void callFragment(Fragment fragment, Context context, String TAG) {
        FragmentManager fragmentManager = ((AppCompatActivity) context).getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
        if (fragment.isAdded()) {
            fragmentTransaction.show(fragment);
        } else {
            //   fragmentTransaction.setCustomAnimations(R.anim.fade_in, R.anim.face_out);
            fragmentTransaction.setCustomAnimations(R.anim.fragment_slide_top_enter, R.anim.fragment_slide_top_exit, R.anim.fragment_slide_bottom_enter, R.anim.fragment_slide_bottom_exit);
            fragmentTransaction.replace(R.id.content_frame, fragment);
            fragmentTransaction.addToBackStack(TAG);
        }
        fragmentTransaction.commit();
    }
    

    并且还为您的片段布局文件提供背景。

    【讨论】:

      【解决方案2】:

      片段默认是透明的。因此,您需要为搜索结果片段提供背景。在布局的根目录上设置背景。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多