简体   繁体   English

Android:清除堆栈后如何从上一个活动恢复

[英]Android : How to resume from last activity after clearing stack

I have 3 activities LandingActivity -> LoginActivity -> MainActivity I have a login button in Landing activity that starts LoginActivity which takes me to MainActivity after successful login , I cleared the task in LoginActivity so when I press back button on MainActivity the app goes to backroung since it is the root in the task the problem is when I resume it starts from LandingActivity how can I fix that to make it resume from MainActivity 我有3个活动LandingActivity-> LoginActivity-> MainActivity我在Landing活动中有一个登录按钮,该按钮启动LoginActivity,成功登录后将我带到MainActivity,我清除了LoginActivity中的任务,因此当我按MainActivity上的向后按钮时,应用程序将返回backroung由于它是任务的根源,问题是当我从LandingActivity开始恢复时,如何解决该问题,使其从MainActivity恢复

AndroidManifest AndroidManifest

<activity android:name=".activity.LandingActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity android:name=".activity.MainActivity"/>
    <activity android:name=".activity.SignUpActivity"/>
    <activity android:name=".activity.LoginActivity"/>

intent used in login button 登录按钮中使用的意图

val intent = Intent(this, MainActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK )
startActivity(intent)

When there's no activites in the stack, like if you press back until you return to home, and you click on the launcher icon, it will always launch the activity with the launcher intent-filter, no matter what activity was open last or if the app process is still alive or not. 当堆栈中没有活动时,例如如果您按回去直到回到家,然后单击启动器图标,则无论最后打开了哪个活动,或者是否打开了活动器,它都将始终使用启动器意图过滤器启动活动。应用程序进程是否仍然存在。 As @TheHebrewHammer suggests, to get around that your LandingActivity can act as the navigation decision tree based on saved data and launch activities. 正如@TheHebrewHammer所建议的那样,要解决此问题,您的LandingActivity可以基于已保存的数据和启动活动充当导航决策树。 You can check how Google handled something similar in the Google I/O Schedule app here 您可以在此处通过Google I / O Schedule应用程序检查Google如何处理类似的操作

Alternatively, if your LandingActivity doesn't show much UI and just asks as a segway to other activities, you could avoid multiple activities most of the time by declaring your MainActivity as the launcher and check for a logged in session like below: 另外,如果您的LandingActivity并没有显示太多UI,而只是作为其他活动的提示,您可以通过将MainActivity声明为启动器并检查如下所示的登录会话来避免大多数情况下的多个活动:

override fun onCreate(savedInstanceState: Bundle?) {
    if (!isUserLoggedIn()) {
        val intent = Intent(this, LoginActivity::class.java)
        startActivity(intent)
        finish()
        return
    }
    // continue as normal
}

If a logged in session in the majority case, you'll only start one activity most of the time, and you won't need to pass along information through intent data from a LauncherActivity in some situations. 如果大多数情况下是登录会话,则大多数情况下您只会启动一个活动,在某些情况下,您不需要通过LauncherActivity的意图数据传递信息。

What you want to do is save a boolean in your shared preferences that saves the state of being logged in. In LandingActivity 's onCreate check that boolean, if it's true just clear the task and jump to your MainActivity no UI will show and it'll look like the user just went directly to the main activity. 您想要做的是在共享的首选项中保存一个布尔值,以保存登录状态。在LandingActivityonCreate检查该布尔值(如果为真),只需清除任务并跳转到MainActivity就不会显示任何UI,并且它会显示。看起来用户只是直接进入了主要活动。 If you then implement logout your app will revert to the old behavior automatically. 如果您随后实施注销,则您的应用将自动恢复为旧行为。

Login button behavior: 登录按钮行为:

PreferenceManager.getDefaultSharedPreferences(this)
        .edit()
        .putBoolean("is_logged_in", true)
        .apply()
val intent = Intent(this, MainActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or 
Intent.FLAG_ACTIVITY_CLEAR_TASK )
startActivity(intent)

LandingActivity's onCreate: LandingActivity的onCreate:

override fun onCreate(savedInstanceState: Bundle?) {
    val isLoggedIn = PreferenceManager.getDefaultSharedPreferences(this)
            .getBoolean("is_logged_in", false)
    if (isLoggedIn) {
        val intent = Intent(this, MainActivity::class.java)
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or 
        Intent.FLAG_ACTIVITY_CLEAR_TASK )
        startActivity(intent)
        return
    }
    // Your normal initialization code here...
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM