如何导航回多个屏幕?

How does one navigate back more than one screen?

我一直在尝试新的导航组件,到目前为止一切顺利。但是,当涉及到以下内容时,我遇到了困难。我的问题最好用一个例子来描述,所以让我举一个例子。

我认为这是一个非常常见的场景,应用程序有一个登录屏幕,然后它们将用户转发到主屏幕、仪表板或类似的东西。让我们坚持命名 - LoginScreenHomeScreen.

说应用很简单。它从 LoginScreen 开始,一旦用户登录,他们就会被转发到 HoneScreen。非常简单的应用程序,但提供了示例。

现在,如果用户回击,我们如何退出应用程序?到目前为止,它总是带我到 LoginScreen

进一步发展这个例子。想象一下,在登录屏幕之前有一个欢迎屏幕,用户可以在其中决定登录或注册。在这种情况下,如何处理返回导航?

本质上,我想问的是导航组件中是否有内置功能可以让您返回多个屏幕,如果没有,有没有办法实现这一点?非常感谢。

你可以试试这个:

Intent myIntent = new Intent(LoginScreen.this, HomeScreen.class);
startActivity(myIntent);
finish(); //finish LoginScreen, then when press back in HomeScreen, it will exit instead return LoginScreen

@Fred

To achieve this you can finish each activity after having started the other one. For example the WelcomeScreen has called LoginScreen you can directly finish it and when LoginScreen calls HomeScreen finish LoginScreen so when the user will navigate back from HomeScreen all activities will be closed.

Or, from the present activity you can call System.exit(0) to exit the application. This serves when you have many activities but if you have one there will be no difference with finish()

Another scenario is as you described in comments: Consider you have following activities: ActivityA, ActivityB and ActivityC and you want to come back to ActivityA from ActivityC without passing by ActivityB. If you ActivityA has been previously opened you can bring it back to front, depending on when you want this to be done. With our sample we consider when the activity finishes:

    @Override
    protected void onDestroy() {
        super.onDestroy();

        //this will bring the ActivityA on the front

        Intent intent=new Intent(this,ActivityA.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        startActivity(intent);

    }

Or you may have a different scenario where the LoginActivity is started only if the is not already connected otherwise you do directly to HomeScreen. In this case, I create a Singleton that will help me register the current calling activity so that I can know which activity will be directly started after successful login.

For example:

if(sharedPreferences!=null) {
                    if (sharedPreferences.getBoolean("user_connected",false)){
                        startActivity(new Intent(this, HomeScreen.class));}
                    else
                    {
                        MySingleton.getInstance().setCurrentCallingActivity("HomeScreen");
                        startActivity(new Intent(this, LoginActivity.class));
                    }
                }

And once in LoginActivity, after a successful login, I do the following:

 try
                                {
                                    startActivity(new Intent(this,
                                            getClassLoader().loadClass(MySingleton.getInstance().getCurrentCallingActivity())));
                                    finish();
                                }catch (ClassNotFoundException e)
                                {
                //your code
                                    finish();
                                }

That's how I handle that and it works. Up to you to see a corresponding scenario and try if it can work. Different approches may coexist and this far from being performant.

首先你需要看看你是否喜欢使用 ActivityFragments.

如果你想删除堆栈中的所有 Fragments 那么你需要使用

 FragmentManager fms = getSupportFragmentManager();
 for (int i = 0; i < fms.getBackStackEntryCount(); ++i) {
                        fms.popBackStack(); }

如果您想删除特定片段,则必须使用标签。

Fragment fragment = getSupportFragmentManager().findFragmentByTag(TAG_FRAGMENT);
if(fragment != null)
getSupportFragmentManager().beginTransaction().remove(fragment).commit();

Now about Activity you need to consider a few options

Activity A 是您的登录名。 Activity B 是您的注册页面。 Activity C 是您的主页。

如果您想在成功登录后从 A 移动到 C,请使用

Intent intent = new Intent(Login.this, MainActivity.class);
startActivity(intent);
finish();

如果您想从 A 移动到 B

Intent intent = new Intent(Login.this, Register.class);
startActivity(intent);

如果你想从 A 移动到 B,注册成功后移动到 C 比使用:

Intent intent = new Intent(LoginSM.this, MainActivitySM.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();