2

我的应用程序有两个活动MAINLOGIN.

MAIN我的应用程序的工作流程如下launcher

 MAIN - If user logged in, show MAIN
 MAIN - If user NOT logged in, show LOGIN and close finish MAIN

下面是一个可以正常工作的场景。

 User launches the app -> sees LOGIN screen
 User logs in, sees the MAIN screen
 User presses back button on phone, sees the phone's screen again (NOT the LOGIN screen again)   

下面是一个不能正常工作的场景。

 User launches the app -> sees LOGIN screen
 Presses middle button on the phone to send app to background
 User launches the app again -> sees the LOGIN screen (here another instance of the login screen is being created...)
 User logs in, sees the MAIN screen
 User presses back button on phone, sees the LOGIN screen again!

我该如何解决这种情况,这样无论手机上的中间按钮是否按下,总是只有一个登录屏幕实例?

这是我的代码:

主要的

       @Override
       public void onStop() {
          super.onStop();
          finish();
       } 

       if (TextUtils.isEmpty(authToken)) {
            Intent login = new Intent(getApplicationContext(), LoginActivity.class);
            startActivityForResult(login, LOGIN_ACTIVITY);
            finish();
        }

登录

    mAccountManager.addAccountExplicitly(account, accountPassword, null);
    mAccountManager.setAuthToken(account, authtokenType, authtoken);
    mAccountManager.setPassword(account, accountPassword);
    startActivity(new Intent(getApplicationContext(), MainActivity.class));

更新

4

1 回答 1

1

为此使用此代码..

 @Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) { 
        // Activity was brought to front and not created, 
        // Thus finishing this will get us to the last viewed activity 
        finish(); 
        return; 
    } 

    // Regular activity creation code... 
} 

此代码始终使应用程序打开您离开的最后一个活动

于 2014-05-09T17:00:02.253 回答