3

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.

  1. User is not logged in.
  2. Launch app -> Launches HomeActivity
  3. No login detected -> Launches LoginActivity
  4. On LoginActivity, press home.
  5. Launch the app, and I see the LoginActivity.
  6. 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.

Ashwini Saini
  • 1,324
  • 11
  • 20
Kumar Bibek
  • 9,016
  • 2
  • 39
  • 68

1 Answers1

-1

I would not stack the activities for this.

If you need to launch the Log In Activity, start the activity and call finish() in your HomeActivity.

if(!loggedIn){
    Intent logInActivity = new Intent(MainActivity.this, LoginActivity.class);
    //Potentially display a toast to say why the user is being directed to the log in screen
    // add bundle into that flags to Go back to main activity. 
   logInActivity.put extra("activity", 1);
    startActivity(logInActivity);
    finish();
}

Similar with the Log-In Activity, after you successfully authenticate, call the following:

if(loggedIn){
    // get bundle it 
      Int a = get intent.getExtras().getInt("activity");
     if(a==1)
    Intent next= new Intent(LoginActivity.this, MainActivity.class);
    ///have a code for each activity that you could resume to
    startActivity(next);
    finish();
}

This should alleviate your state issues.

James
  • 962
  • 5
  • 18
  • That would probably not work in my situation. I have different activities that can launch the LoginActivity, if any of them detect that the user's session has expired. – Kumar Bibek Nov 14 '13 at 09:36
  • If you use bundles you can track the state of your app. See my edit - I wrote this on my commute, so syntaxically not perfect! – James Nov 14 '13 at 09:51
  • Yep, I could do that, but it would probably not be an ideal solution. For now, may be I can use that, but I want to understand why the app behaves this way. – Kumar Bibek Nov 14 '13 at 10:12
  • This might help with your understanding ....http://www.peachpit.com/articles/article.aspx?p=1874864 – James Nov 14 '13 at 10:45