如何停止按下主页按钮时启动的多个活动

小鸟

我的应用程式有两个活动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));

更新

拉尔

为此使用此代码。

 @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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

按下主页按钮时如何使ios应用程序停止

在启动活动中按下后退按钮时如何完成其他活动?

按下按钮时如何停止动画

按下返回按钮时避免启动屏幕活动

按下“停止”按钮时如何停止动画并冻结图像

按下主页按钮后,我得到活动开关ANDROID

单击主页按钮时如何停止服务?

按下后退按钮时如何检查现有活动

按下“设置”按钮时如何显示设置活动?

Ionic 3 - 启动屏幕隐藏在主页按钮按下

按下后退按钮后如何在循环时停止

用户停止按下按钮时如何运行代码?

按下asp按钮时如何停止重新加载页面?

每次按下按钮时如何停止面板加载

如何避免在主页片段中按下后退按钮时出现近 1 秒的延迟

按下按钮时随机活动

按下“后退”或“主页”按钮时,代码仍在运行

按下主页按钮时播放Webview视频

当用户按下“后退”按钮或做出新的意图时,停止位置侦听器活动

按钮停止时按下(波纹)动画

按下主页按钮后如何继续录制?

按下主页按钮后,如何重新加载Web视图?

如何检测科尔多瓦按下的主页按钮?

即使按下“主页”按钮,如何继续运行IntentService

按下按钮时弹出多个框架

按下辅助按钮时,关闭按钮的活动状态

如何在按下时更改按钮背景并在停止按下后继续更改?

区分按下“主页”按钮和打开另一个活动

按下向上按钮时如何向父活动发送数据