【问题标题】:Android popper navigation with Navigation component带有 Navigation 组件的 Android popper 导航
【发布时间】:2020-05-06 01:23:10
【问题描述】:

我有两个片段 FirstFragmentWelcomeFragment

FirstFragment 我有一些动画,我从服务器加载数据,然后我使用 NavController 导航到 WelcomeFragment

navController.navigate(R.id.action_firstFragment_to_welcomeFragment);

我的 nav_graph 是这样的:

<fragment
        android:id="@+id/firstFragment"
        android:name="com.flyeducation.UI.FirstFragment"
        android:label="fragment_first"
        tools:layout="@layout/fragment_first">
        <action
            android:id="@+id/action_firstFragment_to_welcomeFragment"
            app:destination="@id/welcomeFragment"
            app:enterAnim="@anim/fade_in"
            app:exitAnim="@anim/fade_out"
            app:popEnterAnim="@anim/fade_in"
            app:popExitAnim="@anim/fade_out" />
    </fragment>

我不想有这种行为:

从 FirstFragment 导航到 WelcomeFragment 但在按下后退按钮时无法返回到 FirstFragment 应用程序应该像按下主页按钮一样关闭,并且在返回时停留在 WelcomeFragment

来自我在 nav_graph XML 中尝试过的文档:

app:popUpTo="@id/firstFragment"
app:popUpToInclusive="true"

应用程序关闭但当我回到它时,它确实从 FirstFragment 重新开始,而不是保持我想要的状态,即停留在 WelcomeFragment 中

从其他问题我也在 WelcomeFragment 中尝试过这个:

navController.popBackStack(R.id.firstFragment, true);

任何人都可以帮助我实现想要的行为

编辑:

我最终拦截了 onKeyDown 并在我的 WelcomeFragment

上按下 onKeyDown 时模拟主页点击

这在我的 WelcomeFragment

public void myOnKeyDown(int key_code, Context context){
        openDialog(context);
    }

    public void openDialog(final Context context) {
            new AlertDialog.Builder(context)
                    .setTitle("Exit ! ")
                    .setMessage("Are you sure you wanna quit our App , we will miss you !")
                    .setNegativeButton(android.R.string.cancel, null) // dismisses by default
                    .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Intent i = new Intent(Intent.ACTION_MAIN);
                            i.addCategory(Intent.CATEGORY_HOME);
                            context.startActivity(i);
                        }
                    })
                    .create()
                    .show();
        }

这在我的 Activity 中:

@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        ((WelcomeFragment) WelcomeFragment).myOnKeyDown(keyCode, this);
        return false ;
    }

这给了我想要的行为,但我认为这不是最佳做法

【问题讨论】:

    标签: android android-fragments navigation fragment android-architecture-navigation


    【解决方案1】:

    您永远不应该弹出图表的起始位置。根据Principles of Navigation

    注意:应用可能具有一次性设置或一系列登录屏幕。这些conditional screens 不应被视为起始目的地,因为用户仅在某些情况下才能看到这些屏幕。

    您应该改为按照first time user experience documentation 将用户从您的WelcomeFragment 重定向到您的FirstFragment,仅在用户尚未通过该屏幕的情况下。

    通过遵循此设置,99% 的用户可以获得更快的体验(他们只加载一个片段),依赖于您的 startDestinationNavigationUI 帮助程序正常工作,deep links 也将正常工作.

    【讨论】:

    • 我去点赞并简要阅读了它们,就我而言,FirstFragment 始终是我的第一页,因为我在该片段中从服务器加载数据,而 WelcomeFragment 是我检查用户是否经过身份验证的地方
    • @waghydjemy - 这些听起来不像用户在登录后看到的固定起始目的地。这似乎完全符合条件。
    • 你的问题中提供的链接真的很有帮助,但我没有在我的应用程序中遵循这些导航逻辑,我有自己的,非常感谢
    【解决方案2】:

    所以这是在我的情况下使用的正确解决方案,它将具有问题中描述的相同行为,就像我在MainActivity 中覆盖onKeyDown 时所遇到的一样

    OnBackPressedCallback callback = new OnBackPressedCallback(true /* enabled by default */) {
                @Override
                public void handleOnBackPressed() {
                    openDialog(getContext());
                }
            };
            requireActivity().getOnBackPressedDispatcher().addCallback(getViewLifecycleOwner(), callback);
    

    另一个线索:

    app:popUpTo="@id/fragment" 
    

    片段在后栈时使用

    app:popUpToInclusive="true"
    

    将清除堆栈,直到遇到app:popUpTo 中的片段 id

    这里描述popUpTo and popUpToInclusive

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-22
      • 2020-01-15
      • 1970-01-01
      • 1970-01-01
      • 2019-07-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多