我的应用程式有两个活动MAIN
和LOGIN
。
我的应用程序的工作流程是如下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));
更新
为此使用此代码。
@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...
}
此代码始终使应用打开您离开的最后一个活动
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句