Reputation: 412
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
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)
Upvotes: 0
Views: 646
Reputation: 130
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. You can check how Google handled something similar in the Google I/O Schedule app here
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:
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.
Upvotes: 2
Reputation: 3138
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. 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:
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...
}
Upvotes: 1