I have a launcher Activity called HomeActivity
.
And a LoginActivity
from where the user has to login to access the other screens.
HomeActivity
is marked as singleTask
.
In HomeActivity's onCreate()
method, I launch the LoginActivity
if the user is not logged in.
And in LoginActivity
, I just call a finish()
, to dismiss the LoginActivity
, assuming that the HomeActivity
is the next activity in the stack to be shown.
This setup works for most of the cases, except one.
- User is not logged in.
- Launch app -> Launches
HomeActivity
- No login detected -> Launches
LoginActivity
- On
LoginActivity
, press home. - Launch the app, and I see the
LoginActivity
. - Do a login, and on success,
LoginActivity
is finished.
Instead of seeing the HomeActivity
, I see the HomeScreen. Is that expected? Am I doing anything wrong?
I don't have the developer option "Don't keep activities" turned off. So, was wondering how the activity stack loses the HomeActivity
, since I am expecting the HomeActivity
to be the next Activity to come up after I finish the LoginActivity
.
Cancel or Login methods in LoginActivity:
public void doLogin(View v) {
setResult(RESULT_OK);
finish();
}
public void cancel(View v) {
setResult(RESULT_CANCELED);
finish();
}
Calling LoginActivity:
Called from onCreate()
and onNewIntent()
.
Why onNewIntent() is needed? So that, from anywhere else, I can just start the HomeActivity
, and onNewIntent()
would be called, in case of session expiry. All the other activities on top of HomeActivity
would be removed, and LoginActivity
should be shown.
protected void onCreated(Bundle savedInstance){
if(!isLoggedIn()){
startLoginActivity();
}
}
protected void onNewIntent(Intent intent){
if(!isLoggedIn()){
startLoginActivity();
}
}
private void startLoginActivity(){
Intent intent = new Intent(this, LoginActivity.class);
startActivityForResult(intent, 100);
}
On Activity Result of HomeActivity:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode == RESULT_CANCELED){
finish();
}
super.onActivityResult(requestCode, resultCode, data);
}
The manifest file:
<activity android:launchMode="singleTask"
android:name="com.example.checkact.HomeActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.example.checkact.LoginActivity"></activity>
Update: Using singleTop
seems to work fine, but I have no idea why does it behave like this with singleTask
.