alockrem
alockrem

Reputation: 767

Android Activity Lifecycle - Notification Service

I understand the concept of the Android lifecycle and the different events involved. I have created a notification service to let the user know when it's his/her turn. I only want this to execute when the user is no longer viewing one of the activities within the application. He/she will know if it's his/her turn while using the application.

Questions:

Any help is appreciated.

Upvotes: 2

Views: 1918

Answers (2)

Aswin Kumar
Aswin Kumar

Reputation: 5278

  1. Say you are in Activity-A, and you move to Activity-B, then lifecycle callbacks of Activty-A will be called - in this case Activity-A.onPause() immediately, and Activity-A.onDestroy() maybe in a shortwhile if Activity-A is destroyed in that while.

  2. The correct methods to use for starting/stopping your service are onResume() (instead of the onRestart() that you use) and onPause() (instead of the onStop() that you use)

Upvotes: 1

Marcin Orlowski
Marcin Orlowski

Reputation: 75647

You can set up a counter, increment it in Activity's onResume() and decrement on onPause(). If your counter is 0 then it means user is NOT in your activity (is using other app). If it is not 1, then s/he is using your app. Note your counter should be kept outside of activity as it may be removed. I subclass Application class and keep my counters of that global scope there.

Upvotes: 0

Related Questions