Issue
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:
If I setup the lifecycle events on the Main.java class, what happens when the user navigates to a different class? Do the onPause events fire? How does the system know which onPause events to execute since the user could be leaving 10 or more activities during a single session?
I am currently starting the service and stopping the service during the onStop and onRestart events in the Main.java class. This is not working as expected. The notifications occur even when I'm in the system, which is causing the system to lockup because of simultaneous calls to the external apis. (see below).
public void onRestart() { super.onRestart(); Intent i = new Intent(Main.this, NotifyService.class); Main.this.stopService(i);
}
public void onStop() { super.onStop(); Intent i = new Intent(Main.this, NotifyService.class); i.putExtra("UserId", userId); Main.this.startService(i);
}
Any help is appreciated.
Solution
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.
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)
Answered By - Aswin Kumar
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.