Issue
I've got an Android wear class that extends WearableListenerService and which is creating an activity. The documentation says this about activities and tasks upon creation of the activity:
A new activity is, by default, launched into the task of the activity that called startActivity().
Therefore my activity is being added to the WearableListenerService task.
I have no control over the lifetime of the WearableListenerService - the OS calls its onCreate() and onDestroy() methods whenever the handset sends a message to the wearable.
My question is what happens in this scenario:
1) WearableListenerService onCreate() gets called
2) WearableListenerService onMessageReceived() gets called and the WearableListenerService starts a new activity
3) WearableListenerService onDestroy() gets called, but the activity is still present
4) WearableListenerService onCreate() gets called again
5) WearableListenerService onMessageReceived() gets called again and the activity is started agai
n.
At 5), if the activity from 2) is still present then I don't want to create a new separate activity, I always want a singleton activity.
Usually I could use FLAG_ACTIVITY_CLEAR_TOP, FLAG_ACTIVITY_SINGLE_TOP etc. However as WearableListenerService onDestroy() got called at stage 3) does this mean a new task gets created at stage 4) and thus these flags have no effect?
How can I experiment with this? Is there a programatic way of finding out the identify of a task that an activity is in?
Solution
However as WearableListenerService onDestroy() got called at stage 3) does this mean a new task gets created at stage 4) and thus these flags have no effect?
If you are using, android:launchMode="singleTop"
then if an instance of activity already exists at the top of the current task and system routes intent to this activity, no new instance will be created because it will fire off an onNewIntent()
method instead of creating a new object.
So check with onNewIntent()
method in your Activity.
And be more specific your application task only destroyed or cleared if your application has no any instance running, either any Activity or Service or System requires memory in case of Low Memory Scenario.
Again you can also launch Activity in separate task also with android:launchMode="singleTask"
. So your activity always start on new task and combined with singleTop
always remain in top so you can resume from its previous state instead of created it again.
Answered By - user370305
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.