How to handle code when app is killed by swiping in android?
up vote
77
down vote
favorite
If my app is running and i press home button, the app goes in background. Now if a long press the home button and kill the app by swiping it from the recent app list, none of the events like onPause()
, onStop()
or onDestroy()
gets called rather the process is terminated. So if i want my services to stop, kill notifications and unregister listeners, how can i do that? I read quite a few articles and blogs but didn't get any useful information and I haven't found any documentation about it.
Any help would be appreciated.
Thanks in advance.
android
|
show 1 more comment
up vote
77
down vote
favorite
If my app is running and i press home button, the app goes in background. Now if a long press the home button and kill the app by swiping it from the recent app list, none of the events like onPause()
, onStop()
or onDestroy()
gets called rather the process is terminated. So if i want my services to stop, kill notifications and unregister listeners, how can i do that? I read quite a few articles and blogs but didn't get any useful information and I haven't found any documentation about it.
Any help would be appreciated.
Thanks in advance.
android
Did you find any work around for this?
– MysticMagicϡ
Nov 10 '14 at 10:03
@MysticMagic Nope not yet.
– CodeWarrior
Nov 12 '14 at 6:58
I resolve my issue. Check
– MysticMagicϡ
Nov 12 '14 at 7:07
@MysticMagic That works for a Service, what about cancelling Notifications and unregistering listeners?
– CodeWarrior
Nov 17 '14 at 7:00
You can unregister listeners in onTaskRemoved. Can't you? And same for cancelling notifications
– MysticMagicϡ
Nov 17 '14 at 11:00
|
show 1 more comment
up vote
77
down vote
favorite
up vote
77
down vote
favorite
If my app is running and i press home button, the app goes in background. Now if a long press the home button and kill the app by swiping it from the recent app list, none of the events like onPause()
, onStop()
or onDestroy()
gets called rather the process is terminated. So if i want my services to stop, kill notifications and unregister listeners, how can i do that? I read quite a few articles and blogs but didn't get any useful information and I haven't found any documentation about it.
Any help would be appreciated.
Thanks in advance.
android
If my app is running and i press home button, the app goes in background. Now if a long press the home button and kill the app by swiping it from the recent app list, none of the events like onPause()
, onStop()
or onDestroy()
gets called rather the process is terminated. So if i want my services to stop, kill notifications and unregister listeners, how can i do that? I read quite a few articles and blogs but didn't get any useful information and I haven't found any documentation about it.
Any help would be appreciated.
Thanks in advance.
android
android
asked Oct 24 '13 at 14:08
CodeWarrior
2,45121434
2,45121434
Did you find any work around for this?
– MysticMagicϡ
Nov 10 '14 at 10:03
@MysticMagic Nope not yet.
– CodeWarrior
Nov 12 '14 at 6:58
I resolve my issue. Check
– MysticMagicϡ
Nov 12 '14 at 7:07
@MysticMagic That works for a Service, what about cancelling Notifications and unregistering listeners?
– CodeWarrior
Nov 17 '14 at 7:00
You can unregister listeners in onTaskRemoved. Can't you? And same for cancelling notifications
– MysticMagicϡ
Nov 17 '14 at 11:00
|
show 1 more comment
Did you find any work around for this?
– MysticMagicϡ
Nov 10 '14 at 10:03
@MysticMagic Nope not yet.
– CodeWarrior
Nov 12 '14 at 6:58
I resolve my issue. Check
– MysticMagicϡ
Nov 12 '14 at 7:07
@MysticMagic That works for a Service, what about cancelling Notifications and unregistering listeners?
– CodeWarrior
Nov 17 '14 at 7:00
You can unregister listeners in onTaskRemoved. Can't you? And same for cancelling notifications
– MysticMagicϡ
Nov 17 '14 at 11:00
Did you find any work around for this?
– MysticMagicϡ
Nov 10 '14 at 10:03
Did you find any work around for this?
– MysticMagicϡ
Nov 10 '14 at 10:03
@MysticMagic Nope not yet.
– CodeWarrior
Nov 12 '14 at 6:58
@MysticMagic Nope not yet.
– CodeWarrior
Nov 12 '14 at 6:58
I resolve my issue. Check
– MysticMagicϡ
Nov 12 '14 at 7:07
I resolve my issue. Check
– MysticMagicϡ
Nov 12 '14 at 7:07
@MysticMagic That works for a Service, what about cancelling Notifications and unregistering listeners?
– CodeWarrior
Nov 17 '14 at 7:00
@MysticMagic That works for a Service, what about cancelling Notifications and unregistering listeners?
– CodeWarrior
Nov 17 '14 at 7:00
You can unregister listeners in onTaskRemoved. Can't you? And same for cancelling notifications
– MysticMagicϡ
Nov 17 '14 at 11:00
You can unregister listeners in onTaskRemoved. Can't you? And same for cancelling notifications
– MysticMagicϡ
Nov 17 '14 at 11:00
|
show 1 more comment
6 Answers
6
active
oldest
votes
up vote
118
down vote
accepted
I just resolved a similar kind of issue.
Here is what you can do if its just about stopping service when application is killed by swiping from Recent app list.
Inside your Manifest file, keep flag stopWithTask
as true
for Service. Like:
<service
android:name="com.myapp.MyService"
android:stopWithTask="true" />
But as you say you want to unregister listeners and stop notification etc, I would suggest this approach:
Inside your Manifest file, keep flag
stopWithTask
asfalse
for Service. Like:
<service
android:name="com.myapp.MyService"
android:stopWithTask="false" />
Now in your
MyService
service, override methodonTaskRemoved
. (This will be fired only ifstopWithTask
is set tofalse
).
public void onTaskRemoved(Intent rootIntent) {
//unregister listeners
//do any other cleanup if required
//stop service
stopSelf();
}
Refer my question for more details, which contains other part of code, too.
Hope this helps.
Thanks. Thinking if this method could be used to identify swipe kills. Simple blank service ?
– Kiran
Nov 21 '14 at 8:36
Yes @Kiran you can try. :)
– MysticMagicϡ
Nov 21 '14 at 8:37
@MysticMagicϡ but the onTaskRemoved sometimes called after onCreate when service starts again... stackoverflow.com/questions/32224233/…
– umesh
Aug 27 '15 at 7:55
1
@MysticMagicϡ hi, one question, I tried using your solution, but it seems long operations like sending data to server, analytics to google etc some times fail to go all the way through. Could it be that the process is killed before this method had a chance to fully complete itself? Also have you tried this method on an Huawei device? It seems onTaskRemoved never gets called on those devices. Any ideas why?
– Alon Minski
Dec 1 '16 at 8:08
2
@MysticMagicϡ public void onTaskRemoved(..) Method not call in Android O. Working fine in other OS but getting issue in Android O.
– Jay Patel
Apr 26 at 6:42
|
show 5 more comments
up vote
22
down vote
Found one way to do this
make one service like this
public class OnClearFromRecentService extends Service {
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("ClearFromRecentService", "Service Started");
return START_NOT_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d("ClearFromRecentService", "Service Destroyed");
}
@Override
public void onTaskRemoved(Intent rootIntent) {
Log.e("ClearFromRecentService", "END");
//Code here
stopSelf();
}
}
2)register this service in manifest.xml
<service android:name="com.example.OnClearFromRecentService" android:stopWithTask="false" />
3) Then start this service on your splash activity
startService(new Intent(getBaseContext(), OnClearFromRecentService.class));
And now whenever you will clear your app from android recent Then this method onTaskRemoved() will execute.
Awesome solution
– zulkarnain shah
Jun 20 at 9:41
Thanks @zulkarnainshah
– emilpmp
Jun 20 at 10:05
It worked like a charm for me!
– Kwnstantinos Nikoloutsos
Jul 19 at 17:57
Very nice solution!! Thanks!!
– MauroAlexandro
Sep 4 at 10:26
add a comment |
up vote
9
down vote
I resolved similar issue. If you want after swiping from recent task and on next launch it to behave properly then follow below steps:-
1) Save process ID in shared preference:
SharedPreferencesUtils.getInstance().putInt(SharedPreferencesUtils.APP_PROCESS_ID, android.os.Process.myPid());
2) When application is launched from launcher after clear from recent task then do:
int previousProcessID = mSharedPreferencesUtils.getInt(SharedPreferencesUtils.APP_PROCESS_ID);
int currentProcessID = android.os.Process.myPid();
if ((previousProcessID == currentProcessID)) {
// This ensures application not killed yet either by clearing recent or anyway
} else {
// This ensures application killed either by clearing recent or by anyother means
}
Confirmed - used on Android 8 without service.onTaskRemoved() - nicely done
– Gal Rom
May 1 at 13:06
add a comment |
up vote
5
down vote
When you press home - onPause
and onStop
of your Activity is being called, so at this time you have to do all savings and cleanup, because Android platform doesn't further guarantee that onDestroy
or any other lifecycle method would be invoked, so the process could be killed without any notification.
12
Thanks for the suggestion,but i do not wanna kill my notifications, services and listeners, ononPause()
andonstop()
they need to be killed only when the user exits the app i.e. log out.
– CodeWarrior
Oct 25 '13 at 4:32
add a comment |
up vote
1
down vote
You need to save your data when on onPause()
is called.
Look at this life cycle diagram:
Android Developer
You can see that an app can be killed after onPause()
or onStop()
.
Handle your data there and recover it in onRestart()
onCreate()
.
good luck!
add a comment |
up vote
0
down vote
You can't handle swipe, because system just removes your process from memory without calling any callback.
I have checked, that before user calls "recent apps" screen, onPause() will be always called. So you need to save all data in onPause method without checking isFinishing().
To check back button, use onBackPressed method.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f19568315%2fhow-to-handle-code-when-app-is-killed-by-swiping-in-android%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
118
down vote
accepted
I just resolved a similar kind of issue.
Here is what you can do if its just about stopping service when application is killed by swiping from Recent app list.
Inside your Manifest file, keep flag stopWithTask
as true
for Service. Like:
<service
android:name="com.myapp.MyService"
android:stopWithTask="true" />
But as you say you want to unregister listeners and stop notification etc, I would suggest this approach:
Inside your Manifest file, keep flag
stopWithTask
asfalse
for Service. Like:
<service
android:name="com.myapp.MyService"
android:stopWithTask="false" />
Now in your
MyService
service, override methodonTaskRemoved
. (This will be fired only ifstopWithTask
is set tofalse
).
public void onTaskRemoved(Intent rootIntent) {
//unregister listeners
//do any other cleanup if required
//stop service
stopSelf();
}
Refer my question for more details, which contains other part of code, too.
Hope this helps.
Thanks. Thinking if this method could be used to identify swipe kills. Simple blank service ?
– Kiran
Nov 21 '14 at 8:36
Yes @Kiran you can try. :)
– MysticMagicϡ
Nov 21 '14 at 8:37
@MysticMagicϡ but the onTaskRemoved sometimes called after onCreate when service starts again... stackoverflow.com/questions/32224233/…
– umesh
Aug 27 '15 at 7:55
1
@MysticMagicϡ hi, one question, I tried using your solution, but it seems long operations like sending data to server, analytics to google etc some times fail to go all the way through. Could it be that the process is killed before this method had a chance to fully complete itself? Also have you tried this method on an Huawei device? It seems onTaskRemoved never gets called on those devices. Any ideas why?
– Alon Minski
Dec 1 '16 at 8:08
2
@MysticMagicϡ public void onTaskRemoved(..) Method not call in Android O. Working fine in other OS but getting issue in Android O.
– Jay Patel
Apr 26 at 6:42
|
show 5 more comments
up vote
118
down vote
accepted
I just resolved a similar kind of issue.
Here is what you can do if its just about stopping service when application is killed by swiping from Recent app list.
Inside your Manifest file, keep flag stopWithTask
as true
for Service. Like:
<service
android:name="com.myapp.MyService"
android:stopWithTask="true" />
But as you say you want to unregister listeners and stop notification etc, I would suggest this approach:
Inside your Manifest file, keep flag
stopWithTask
asfalse
for Service. Like:
<service
android:name="com.myapp.MyService"
android:stopWithTask="false" />
Now in your
MyService
service, override methodonTaskRemoved
. (This will be fired only ifstopWithTask
is set tofalse
).
public void onTaskRemoved(Intent rootIntent) {
//unregister listeners
//do any other cleanup if required
//stop service
stopSelf();
}
Refer my question for more details, which contains other part of code, too.
Hope this helps.
Thanks. Thinking if this method could be used to identify swipe kills. Simple blank service ?
– Kiran
Nov 21 '14 at 8:36
Yes @Kiran you can try. :)
– MysticMagicϡ
Nov 21 '14 at 8:37
@MysticMagicϡ but the onTaskRemoved sometimes called after onCreate when service starts again... stackoverflow.com/questions/32224233/…
– umesh
Aug 27 '15 at 7:55
1
@MysticMagicϡ hi, one question, I tried using your solution, but it seems long operations like sending data to server, analytics to google etc some times fail to go all the way through. Could it be that the process is killed before this method had a chance to fully complete itself? Also have you tried this method on an Huawei device? It seems onTaskRemoved never gets called on those devices. Any ideas why?
– Alon Minski
Dec 1 '16 at 8:08
2
@MysticMagicϡ public void onTaskRemoved(..) Method not call in Android O. Working fine in other OS but getting issue in Android O.
– Jay Patel
Apr 26 at 6:42
|
show 5 more comments
up vote
118
down vote
accepted
up vote
118
down vote
accepted
I just resolved a similar kind of issue.
Here is what you can do if its just about stopping service when application is killed by swiping from Recent app list.
Inside your Manifest file, keep flag stopWithTask
as true
for Service. Like:
<service
android:name="com.myapp.MyService"
android:stopWithTask="true" />
But as you say you want to unregister listeners and stop notification etc, I would suggest this approach:
Inside your Manifest file, keep flag
stopWithTask
asfalse
for Service. Like:
<service
android:name="com.myapp.MyService"
android:stopWithTask="false" />
Now in your
MyService
service, override methodonTaskRemoved
. (This will be fired only ifstopWithTask
is set tofalse
).
public void onTaskRemoved(Intent rootIntent) {
//unregister listeners
//do any other cleanup if required
//stop service
stopSelf();
}
Refer my question for more details, which contains other part of code, too.
Hope this helps.
I just resolved a similar kind of issue.
Here is what you can do if its just about stopping service when application is killed by swiping from Recent app list.
Inside your Manifest file, keep flag stopWithTask
as true
for Service. Like:
<service
android:name="com.myapp.MyService"
android:stopWithTask="true" />
But as you say you want to unregister listeners and stop notification etc, I would suggest this approach:
Inside your Manifest file, keep flag
stopWithTask
asfalse
for Service. Like:
<service
android:name="com.myapp.MyService"
android:stopWithTask="false" />
Now in your
MyService
service, override methodonTaskRemoved
. (This will be fired only ifstopWithTask
is set tofalse
).
public void onTaskRemoved(Intent rootIntent) {
//unregister listeners
//do any other cleanup if required
//stop service
stopSelf();
}
Refer my question for more details, which contains other part of code, too.
Hope this helps.
edited May 23 '17 at 11:55
Community♦
11
11
answered Nov 12 '14 at 8:38
MysticMagicϡ
22.7k135694
22.7k135694
Thanks. Thinking if this method could be used to identify swipe kills. Simple blank service ?
– Kiran
Nov 21 '14 at 8:36
Yes @Kiran you can try. :)
– MysticMagicϡ
Nov 21 '14 at 8:37
@MysticMagicϡ but the onTaskRemoved sometimes called after onCreate when service starts again... stackoverflow.com/questions/32224233/…
– umesh
Aug 27 '15 at 7:55
1
@MysticMagicϡ hi, one question, I tried using your solution, but it seems long operations like sending data to server, analytics to google etc some times fail to go all the way through. Could it be that the process is killed before this method had a chance to fully complete itself? Also have you tried this method on an Huawei device? It seems onTaskRemoved never gets called on those devices. Any ideas why?
– Alon Minski
Dec 1 '16 at 8:08
2
@MysticMagicϡ public void onTaskRemoved(..) Method not call in Android O. Working fine in other OS but getting issue in Android O.
– Jay Patel
Apr 26 at 6:42
|
show 5 more comments
Thanks. Thinking if this method could be used to identify swipe kills. Simple blank service ?
– Kiran
Nov 21 '14 at 8:36
Yes @Kiran you can try. :)
– MysticMagicϡ
Nov 21 '14 at 8:37
@MysticMagicϡ but the onTaskRemoved sometimes called after onCreate when service starts again... stackoverflow.com/questions/32224233/…
– umesh
Aug 27 '15 at 7:55
1
@MysticMagicϡ hi, one question, I tried using your solution, but it seems long operations like sending data to server, analytics to google etc some times fail to go all the way through. Could it be that the process is killed before this method had a chance to fully complete itself? Also have you tried this method on an Huawei device? It seems onTaskRemoved never gets called on those devices. Any ideas why?
– Alon Minski
Dec 1 '16 at 8:08
2
@MysticMagicϡ public void onTaskRemoved(..) Method not call in Android O. Working fine in other OS but getting issue in Android O.
– Jay Patel
Apr 26 at 6:42
Thanks. Thinking if this method could be used to identify swipe kills. Simple blank service ?
– Kiran
Nov 21 '14 at 8:36
Thanks. Thinking if this method could be used to identify swipe kills. Simple blank service ?
– Kiran
Nov 21 '14 at 8:36
Yes @Kiran you can try. :)
– MysticMagicϡ
Nov 21 '14 at 8:37
Yes @Kiran you can try. :)
– MysticMagicϡ
Nov 21 '14 at 8:37
@MysticMagicϡ but the onTaskRemoved sometimes called after onCreate when service starts again... stackoverflow.com/questions/32224233/…
– umesh
Aug 27 '15 at 7:55
@MysticMagicϡ but the onTaskRemoved sometimes called after onCreate when service starts again... stackoverflow.com/questions/32224233/…
– umesh
Aug 27 '15 at 7:55
1
1
@MysticMagicϡ hi, one question, I tried using your solution, but it seems long operations like sending data to server, analytics to google etc some times fail to go all the way through. Could it be that the process is killed before this method had a chance to fully complete itself? Also have you tried this method on an Huawei device? It seems onTaskRemoved never gets called on those devices. Any ideas why?
– Alon Minski
Dec 1 '16 at 8:08
@MysticMagicϡ hi, one question, I tried using your solution, but it seems long operations like sending data to server, analytics to google etc some times fail to go all the way through. Could it be that the process is killed before this method had a chance to fully complete itself? Also have you tried this method on an Huawei device? It seems onTaskRemoved never gets called on those devices. Any ideas why?
– Alon Minski
Dec 1 '16 at 8:08
2
2
@MysticMagicϡ public void onTaskRemoved(..) Method not call in Android O. Working fine in other OS but getting issue in Android O.
– Jay Patel
Apr 26 at 6:42
@MysticMagicϡ public void onTaskRemoved(..) Method not call in Android O. Working fine in other OS but getting issue in Android O.
– Jay Patel
Apr 26 at 6:42
|
show 5 more comments
up vote
22
down vote
Found one way to do this
make one service like this
public class OnClearFromRecentService extends Service {
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("ClearFromRecentService", "Service Started");
return START_NOT_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d("ClearFromRecentService", "Service Destroyed");
}
@Override
public void onTaskRemoved(Intent rootIntent) {
Log.e("ClearFromRecentService", "END");
//Code here
stopSelf();
}
}
2)register this service in manifest.xml
<service android:name="com.example.OnClearFromRecentService" android:stopWithTask="false" />
3) Then start this service on your splash activity
startService(new Intent(getBaseContext(), OnClearFromRecentService.class));
And now whenever you will clear your app from android recent Then this method onTaskRemoved() will execute.
Awesome solution
– zulkarnain shah
Jun 20 at 9:41
Thanks @zulkarnainshah
– emilpmp
Jun 20 at 10:05
It worked like a charm for me!
– Kwnstantinos Nikoloutsos
Jul 19 at 17:57
Very nice solution!! Thanks!!
– MauroAlexandro
Sep 4 at 10:26
add a comment |
up vote
22
down vote
Found one way to do this
make one service like this
public class OnClearFromRecentService extends Service {
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("ClearFromRecentService", "Service Started");
return START_NOT_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d("ClearFromRecentService", "Service Destroyed");
}
@Override
public void onTaskRemoved(Intent rootIntent) {
Log.e("ClearFromRecentService", "END");
//Code here
stopSelf();
}
}
2)register this service in manifest.xml
<service android:name="com.example.OnClearFromRecentService" android:stopWithTask="false" />
3) Then start this service on your splash activity
startService(new Intent(getBaseContext(), OnClearFromRecentService.class));
And now whenever you will clear your app from android recent Then this method onTaskRemoved() will execute.
Awesome solution
– zulkarnain shah
Jun 20 at 9:41
Thanks @zulkarnainshah
– emilpmp
Jun 20 at 10:05
It worked like a charm for me!
– Kwnstantinos Nikoloutsos
Jul 19 at 17:57
Very nice solution!! Thanks!!
– MauroAlexandro
Sep 4 at 10:26
add a comment |
up vote
22
down vote
up vote
22
down vote
Found one way to do this
make one service like this
public class OnClearFromRecentService extends Service {
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("ClearFromRecentService", "Service Started");
return START_NOT_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d("ClearFromRecentService", "Service Destroyed");
}
@Override
public void onTaskRemoved(Intent rootIntent) {
Log.e("ClearFromRecentService", "END");
//Code here
stopSelf();
}
}
2)register this service in manifest.xml
<service android:name="com.example.OnClearFromRecentService" android:stopWithTask="false" />
3) Then start this service on your splash activity
startService(new Intent(getBaseContext(), OnClearFromRecentService.class));
And now whenever you will clear your app from android recent Then this method onTaskRemoved() will execute.
Found one way to do this
make one service like this
public class OnClearFromRecentService extends Service {
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("ClearFromRecentService", "Service Started");
return START_NOT_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d("ClearFromRecentService", "Service Destroyed");
}
@Override
public void onTaskRemoved(Intent rootIntent) {
Log.e("ClearFromRecentService", "END");
//Code here
stopSelf();
}
}
2)register this service in manifest.xml
<service android:name="com.example.OnClearFromRecentService" android:stopWithTask="false" />
3) Then start this service on your splash activity
startService(new Intent(getBaseContext(), OnClearFromRecentService.class));
And now whenever you will clear your app from android recent Then this method onTaskRemoved() will execute.
answered Mar 17 '17 at 11:49
emilpmp
599714
599714
Awesome solution
– zulkarnain shah
Jun 20 at 9:41
Thanks @zulkarnainshah
– emilpmp
Jun 20 at 10:05
It worked like a charm for me!
– Kwnstantinos Nikoloutsos
Jul 19 at 17:57
Very nice solution!! Thanks!!
– MauroAlexandro
Sep 4 at 10:26
add a comment |
Awesome solution
– zulkarnain shah
Jun 20 at 9:41
Thanks @zulkarnainshah
– emilpmp
Jun 20 at 10:05
It worked like a charm for me!
– Kwnstantinos Nikoloutsos
Jul 19 at 17:57
Very nice solution!! Thanks!!
– MauroAlexandro
Sep 4 at 10:26
Awesome solution
– zulkarnain shah
Jun 20 at 9:41
Awesome solution
– zulkarnain shah
Jun 20 at 9:41
Thanks @zulkarnainshah
– emilpmp
Jun 20 at 10:05
Thanks @zulkarnainshah
– emilpmp
Jun 20 at 10:05
It worked like a charm for me!
– Kwnstantinos Nikoloutsos
Jul 19 at 17:57
It worked like a charm for me!
– Kwnstantinos Nikoloutsos
Jul 19 at 17:57
Very nice solution!! Thanks!!
– MauroAlexandro
Sep 4 at 10:26
Very nice solution!! Thanks!!
– MauroAlexandro
Sep 4 at 10:26
add a comment |
up vote
9
down vote
I resolved similar issue. If you want after swiping from recent task and on next launch it to behave properly then follow below steps:-
1) Save process ID in shared preference:
SharedPreferencesUtils.getInstance().putInt(SharedPreferencesUtils.APP_PROCESS_ID, android.os.Process.myPid());
2) When application is launched from launcher after clear from recent task then do:
int previousProcessID = mSharedPreferencesUtils.getInt(SharedPreferencesUtils.APP_PROCESS_ID);
int currentProcessID = android.os.Process.myPid();
if ((previousProcessID == currentProcessID)) {
// This ensures application not killed yet either by clearing recent or anyway
} else {
// This ensures application killed either by clearing recent or by anyother means
}
Confirmed - used on Android 8 without service.onTaskRemoved() - nicely done
– Gal Rom
May 1 at 13:06
add a comment |
up vote
9
down vote
I resolved similar issue. If you want after swiping from recent task and on next launch it to behave properly then follow below steps:-
1) Save process ID in shared preference:
SharedPreferencesUtils.getInstance().putInt(SharedPreferencesUtils.APP_PROCESS_ID, android.os.Process.myPid());
2) When application is launched from launcher after clear from recent task then do:
int previousProcessID = mSharedPreferencesUtils.getInt(SharedPreferencesUtils.APP_PROCESS_ID);
int currentProcessID = android.os.Process.myPid();
if ((previousProcessID == currentProcessID)) {
// This ensures application not killed yet either by clearing recent or anyway
} else {
// This ensures application killed either by clearing recent or by anyother means
}
Confirmed - used on Android 8 without service.onTaskRemoved() - nicely done
– Gal Rom
May 1 at 13:06
add a comment |
up vote
9
down vote
up vote
9
down vote
I resolved similar issue. If you want after swiping from recent task and on next launch it to behave properly then follow below steps:-
1) Save process ID in shared preference:
SharedPreferencesUtils.getInstance().putInt(SharedPreferencesUtils.APP_PROCESS_ID, android.os.Process.myPid());
2) When application is launched from launcher after clear from recent task then do:
int previousProcessID = mSharedPreferencesUtils.getInt(SharedPreferencesUtils.APP_PROCESS_ID);
int currentProcessID = android.os.Process.myPid();
if ((previousProcessID == currentProcessID)) {
// This ensures application not killed yet either by clearing recent or anyway
} else {
// This ensures application killed either by clearing recent or by anyother means
}
I resolved similar issue. If you want after swiping from recent task and on next launch it to behave properly then follow below steps:-
1) Save process ID in shared preference:
SharedPreferencesUtils.getInstance().putInt(SharedPreferencesUtils.APP_PROCESS_ID, android.os.Process.myPid());
2) When application is launched from launcher after clear from recent task then do:
int previousProcessID = mSharedPreferencesUtils.getInt(SharedPreferencesUtils.APP_PROCESS_ID);
int currentProcessID = android.os.Process.myPid();
if ((previousProcessID == currentProcessID)) {
// This ensures application not killed yet either by clearing recent or anyway
} else {
// This ensures application killed either by clearing recent or by anyother means
}
edited Oct 7 '15 at 14:51
Peter
1,33532141
1,33532141
answered Oct 7 '15 at 13:34
Anshul Agarwal
754711
754711
Confirmed - used on Android 8 without service.onTaskRemoved() - nicely done
– Gal Rom
May 1 at 13:06
add a comment |
Confirmed - used on Android 8 without service.onTaskRemoved() - nicely done
– Gal Rom
May 1 at 13:06
Confirmed - used on Android 8 without service.onTaskRemoved() - nicely done
– Gal Rom
May 1 at 13:06
Confirmed - used on Android 8 without service.onTaskRemoved() - nicely done
– Gal Rom
May 1 at 13:06
add a comment |
up vote
5
down vote
When you press home - onPause
and onStop
of your Activity is being called, so at this time you have to do all savings and cleanup, because Android platform doesn't further guarantee that onDestroy
or any other lifecycle method would be invoked, so the process could be killed without any notification.
12
Thanks for the suggestion,but i do not wanna kill my notifications, services and listeners, ononPause()
andonstop()
they need to be killed only when the user exits the app i.e. log out.
– CodeWarrior
Oct 25 '13 at 4:32
add a comment |
up vote
5
down vote
When you press home - onPause
and onStop
of your Activity is being called, so at this time you have to do all savings and cleanup, because Android platform doesn't further guarantee that onDestroy
or any other lifecycle method would be invoked, so the process could be killed without any notification.
12
Thanks for the suggestion,but i do not wanna kill my notifications, services and listeners, ononPause()
andonstop()
they need to be killed only when the user exits the app i.e. log out.
– CodeWarrior
Oct 25 '13 at 4:32
add a comment |
up vote
5
down vote
up vote
5
down vote
When you press home - onPause
and onStop
of your Activity is being called, so at this time you have to do all savings and cleanup, because Android platform doesn't further guarantee that onDestroy
or any other lifecycle method would be invoked, so the process could be killed without any notification.
When you press home - onPause
and onStop
of your Activity is being called, so at this time you have to do all savings and cleanup, because Android platform doesn't further guarantee that onDestroy
or any other lifecycle method would be invoked, so the process could be killed without any notification.
answered Oct 24 '13 at 14:16
Desert
2,1131014
2,1131014
12
Thanks for the suggestion,but i do not wanna kill my notifications, services and listeners, ononPause()
andonstop()
they need to be killed only when the user exits the app i.e. log out.
– CodeWarrior
Oct 25 '13 at 4:32
add a comment |
12
Thanks for the suggestion,but i do not wanna kill my notifications, services and listeners, ononPause()
andonstop()
they need to be killed only when the user exits the app i.e. log out.
– CodeWarrior
Oct 25 '13 at 4:32
12
12
Thanks for the suggestion,but i do not wanna kill my notifications, services and listeners, on
onPause()
and onstop()
they need to be killed only when the user exits the app i.e. log out.– CodeWarrior
Oct 25 '13 at 4:32
Thanks for the suggestion,but i do not wanna kill my notifications, services and listeners, on
onPause()
and onstop()
they need to be killed only when the user exits the app i.e. log out.– CodeWarrior
Oct 25 '13 at 4:32
add a comment |
up vote
1
down vote
You need to save your data when on onPause()
is called.
Look at this life cycle diagram:
Android Developer
You can see that an app can be killed after onPause()
or onStop()
.
Handle your data there and recover it in onRestart()
onCreate()
.
good luck!
add a comment |
up vote
1
down vote
You need to save your data when on onPause()
is called.
Look at this life cycle diagram:
Android Developer
You can see that an app can be killed after onPause()
or onStop()
.
Handle your data there and recover it in onRestart()
onCreate()
.
good luck!
add a comment |
up vote
1
down vote
up vote
1
down vote
You need to save your data when on onPause()
is called.
Look at this life cycle diagram:
Android Developer
You can see that an app can be killed after onPause()
or onStop()
.
Handle your data there and recover it in onRestart()
onCreate()
.
good luck!
You need to save your data when on onPause()
is called.
Look at this life cycle diagram:
Android Developer
You can see that an app can be killed after onPause()
or onStop()
.
Handle your data there and recover it in onRestart()
onCreate()
.
good luck!
edited Oct 24 '13 at 14:51
CodeWarrior
2,45121434
2,45121434
answered Oct 24 '13 at 14:33
Fashizel
470617
470617
add a comment |
add a comment |
up vote
0
down vote
You can't handle swipe, because system just removes your process from memory without calling any callback.
I have checked, that before user calls "recent apps" screen, onPause() will be always called. So you need to save all data in onPause method without checking isFinishing().
To check back button, use onBackPressed method.
add a comment |
up vote
0
down vote
You can't handle swipe, because system just removes your process from memory without calling any callback.
I have checked, that before user calls "recent apps" screen, onPause() will be always called. So you need to save all data in onPause method without checking isFinishing().
To check back button, use onBackPressed method.
add a comment |
up vote
0
down vote
up vote
0
down vote
You can't handle swipe, because system just removes your process from memory without calling any callback.
I have checked, that before user calls "recent apps" screen, onPause() will be always called. So you need to save all data in onPause method without checking isFinishing().
To check back button, use onBackPressed method.
You can't handle swipe, because system just removes your process from memory without calling any callback.
I have checked, that before user calls "recent apps" screen, onPause() will be always called. So you need to save all data in onPause method without checking isFinishing().
To check back button, use onBackPressed method.
answered Jul 28 '17 at 10:46
Ahmad
95111
95111
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f19568315%2fhow-to-handle-code-when-app-is-killed-by-swiping-in-android%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Did you find any work around for this?
– MysticMagicϡ
Nov 10 '14 at 10:03
@MysticMagic Nope not yet.
– CodeWarrior
Nov 12 '14 at 6:58
I resolve my issue. Check
– MysticMagicϡ
Nov 12 '14 at 7:07
@MysticMagic That works for a Service, what about cancelling Notifications and unregistering listeners?
– CodeWarrior
Nov 17 '14 at 7:00
You can unregister listeners in onTaskRemoved. Can't you? And same for cancelling notifications
– MysticMagicϡ
Nov 17 '14 at 11:00