开发者

Intent.FLAG_ACTIVITY_CLEAR_TOP causes activity to pop-up even when application is on background?

开发者 https://www.devze.com 2023-04-10 09:55 出处:网络
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

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 applicat开发者_如何学Goion 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);
}


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):

@Override    
     protected void onResume() {   
         super.onResume();   
         if (!getLoggedIn())   
         {  
              startActivity(new Intent(this, LoginActivity.class));   
              finish();   
         }   
     }
0

精彩评论

暂无评论...
验证码 换一张
取 消