0

I want to detect that whether the application is killed by the android system or whether the user killed the app from the recent app list. if the activity was killed in either way I want to clear the cookies in the webview. The webview is part of another activity inside my main launcher activity. Currently I am sending the broadcast to clear the cookies when the onCreate method of the launcher activity is called. but when I am closing the app with the normal way and on starting it again the cookies details are getting cleared(the broadcast is working fine) while if the application is killed from the recent app it seems it is not sending the broadcast even when the onCreate is called.Kindly suggest what I am missing.

Preeti Wadhwani
  • 305
  • 1
  • 3
  • 15
  • Can you please show the code sending broadcast. – Rohan Apr 20 '15 at 11:02
  • @Rohan: here is the code: public static void sendUpdateListBroadCast(String action, Context context, Bundle bundle) { Intent broadcastIntent = new Intent(); broadcastIntent.setAction(action); if (bundle != null) broadcastIntent.putExtra("bundle", bundle); LocalBroadcastManager.getInstance(context).sendBroadcast( broadcastIntent); } – Preeti Wadhwani Apr 20 '15 at 11:10
  • are you sending broadcast from first activity and receiving it in second activity. if so then your second activity will never receive broadcast. because when you kill your app it will kill your second activity too to whom your broadcast is registered – Rohan Apr 20 '15 at 11:35
  • @Rohan: Yes I am doing that.. But when i close the application in a normal way and start it again , the second activity is receiving the broadcast... – Preeti Wadhwani Apr 20 '15 at 11:39
  • are you unregistering your receiver from secondary activity's onStop() or some other event – Rohan Apr 20 '15 at 11:42
  • @Rohan No. I did something else to fix the issue. I am maintaining one global variable isClearCookie and making it true when the application starts again either due to app kill or due to closing of app. I am reading the value in webviewActivity and if it is true i am clearing cookies and then making is false so that it doesnt clear the cookies till the time application is not stopped. It is working fine. Thanks for the help. do u think it is a good approach ? – Preeti Wadhwani Apr 20 '15 at 11:56
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/75710/discussion-between-rohan-and-gpw). – Rohan Apr 20 '15 at 12:03
  • Hi what was the conclusion.....Do we catch the listener when user clear app from recent app list ? – Himanshu Jan 10 '17 at 09:57

3 Answers3

0

It seems you need to clear the cache or cookies on onDestroy or onStop of your main activity.

(Post some code where you have problem with)

suresh
  • 266
  • 1
  • 7
  • yes.. but the problem is main activity can start different activities i want to clear the cookies data when the user is killing the application from the recent apps and that time it can be on activity not necessarily lancher activity – Preeti Wadhwani Apr 20 '15 at 08:34
  • public void onCreate(Bundle bundle) { super.onCreate(bundle); try { Utils.sendUpdateListBroadCast(Constants.Action_CLEAR_COOKIES, this, null); } when the applicaiton is closed and started the broadcast receiver is working fine but while killing from recent apps and aftr restarting the application it is not doing anything – Preeti Wadhwani Apr 20 '15 at 08:37
0

onDestroy and onStop would'nt be fired if app is closed from recent apps or killed by android system.

Here is a workaround to get this done.

Let me know if you have any confusion.

Community
  • 1
  • 1
Waqar Khan
  • 468
  • 4
  • 18
0

I would suggest different way to achieve same. pass data or some flag which will notify your webViewActivity whether to clear your cookies or not here is small code snippet which can help you

//in your launcher Activity

Intent intent=new Intent(LauncherActivity.this,WebViewActivity.class);
intent.putExtra("clear_cookies",true); 
startActivity(intent)

//in your webViewActivity's onCreate

Boolean cookieFlag=false;
    Bundle bundle=getIntent().getExtras();
    if(bundle !=null)
    {
        if(bundle.containsKey("clear_cookies"))
        {
            cookieFlag=bundle.getBoolean("clear_cookies")

        }
    }
    if(cookieFlag)
    {
        //write your code clearing cookie
    }
Rohan
  • 188
  • 4
  • 13