即使应用程序处于后台,Intent.FLAG_ACTIVITY_CLEAR_TOP 也会导致活动弹出?
我使用的是安卓2.2。我有一个应用程序,它会在一段时间不活动后注销(导致应用程序返回登录页面)。我正在将 Intent.FLAG_ACTIVITY_CLEAR_TOP
用于我的 Intent
。但是,我注意到,当我的应用程序处于后台并且在一段时间内处于非活动状态时,登录页面会突然弹出,并且我的应用程序会转到前台。我预计登录页面将保留在后台。当我没有使用任何标志来实现我的意图时,这种情况不会发生。如果我没有为我的意图使用任何标志,登录页面会在后台悄悄启动。但如果不使用 Intent.FLAG_ACTIVITY_CLEAR_TOP
,我将无法清除我的历史堆栈。
为什么会发生这种情况?如何在后台安静地启动活动?
下面是我的代码片段:
@Override //Inside a class extending AsyncTask
protected void onPostExecute(String result)
{
((GlobalApplication)getApplicationContext()).setIsLogin(false); //user is not logged in anymore
Intent intent = new Intent(this, LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
下面是我根据 Lars 的建议编写的代码片段:
@Override //Inside a class extending AsyncTask
protected void onPostExecute(String result)
{
((GlobalApplication)getApplicationContext()).setIsLogin(false); //user is not logged in anymore
Intent intent = new Intent(this, LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
if(((GlobalApplication)getApplicationContext()).isOnBackground())
((GlobalApplication)getApplicationContext()).setPendingIntent(intent);
else
startActivity(intent);
}
@Override //overrides android.app.Activity. inside the current Activity
protected void onResume()
{
super.onResume();
Intent pendingIntent = ((GlobalApplication)getApplicationContext()).getPendingIntent();
if(pendingIntent != null)
startActivity(pendingIntent);
}
I'm using Android 2.2. I have an application which logouts (causing the application to return to the login page) after a certain period of inactivity. I am using Intent.FLAG_ACTIVITY_CLEAR_TOP
for my Intent
. However, I notice that when my application is on background and is inactive for a certain period of time, the login page suddenly pops-up and my application goes to foreground. I am expecting that the login page will remain to background. This does not happen when I am not using any flag for my intent. If I'm not using any flag for my Intent, the login page is quietly started on the background. But without using Intent.FLAG_ACTIVITY_CLEAR_TOP
, I won't be able to clear my history stack.
Why is this happening? How can I launch an activity on background quietly?
Below is a snippet of my code:
@Override //Inside a class extending AsyncTask
protected void onPostExecute(String result)
{
((GlobalApplication)getApplicationContext()).setIsLogin(false); //user is not logged in anymore
Intent intent = new Intent(this, LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
Below is a snippet the code I made from Lars' suggestion:
@Override //Inside a class extending AsyncTask
protected void onPostExecute(String result)
{
((GlobalApplication)getApplicationContext()).setIsLogin(false); //user is not logged in anymore
Intent intent = new Intent(this, LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
if(((GlobalApplication)getApplicationContext()).isOnBackground())
((GlobalApplication)getApplicationContext()).setPendingIntent(intent);
else
startActivity(intent);
}
@Override //overrides android.app.Activity. inside the current Activity
protected void onResume()
{
super.onResume();
Intent pendingIntent = ((GlobalApplication)getApplicationContext()).getPendingIntent();
if(pendingIntent != null)
startActivity(pendingIntent);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否尝试过检查您是否仍在活动的 onResume 部分中登录,而不是在计时器关闭时调用您的 LoginActivity (这就是我假设您正在做的事情)?
编辑:为了澄清起见,您将在预定义的时间段(或事件发生时)后注销用户。此时,您启动一个 AsyncTask,它为您的登录活动创建一个意图,为其添加一个标志并启动它。您的问题是,除非用户打开应用程序,否则您不希望 loginActivity 进入前台。准确吗?
因为如果是这样,我建议使用上面提到的 onResume 方法。每当活动回到(返回)前台时就会调用这些。为了即使用户没有更改活动也显示登录屏幕,您可以尝试发送广播并在活动中收听它。
编辑:我的评论中的代码片段(现在格式正确):
Have you tried checking if you are still logged in in the onResume part of your activities instead of calling your LoginActivity when a timer goes off (which is what I'm assuming you are doing)?
Edit: For clarification, you are logging the user out after a predefined period of time (or when an event occurs). At this point you start an AsyncTask which creates an intent for your loginActivity, adds a flag to it and starts it. And your problem is that you don't want the loginActivity to come to the foreground unless the user has the application open. Is that accurate?
Because if so I would recommend using the onResume methods like I mentioned above. These are called whenever an activity comes (back) to the foreground. For showing the login screen even when the user doesn't change activities you could try sending a broadcast and listening for it in your activities.
EDIT: Code snippet from my comment (Now correctly formatted):