开发者

How can one pass data back to a paused activity from a service through a pending intent?

开发者 https://www.devze.com 2023-03-19 09:56 出处:网络
I have an app that is checking the location of the device in a service and whether it has entered one of many predefined areas.The service has a reference back to the activity when the activity is ope

I have an app that is checking the location of the device in a service and whether it has entered one of many predefined areas. The service has a reference back to the activity when the activity is open and when the activity is put into the background the onPause sets the service's reference the activity to null. When the activity ref is null a notification is sent with a pendingIntent to the activity.

The issue that I am running into is that the when the activity is reopened after clicking on the notification item the onCreate or onRestoreInstanceState methods aren't called and the onResume which is called doesn't have a reference to the area object that was added to the pendingIntent.

I can get things working by adding a flag within the service indicating whether an activity is attached or not rather than setting the activity to null. This allows an activity method to be called that updates the views indicating a defined area has been entered, but this method seems to have issues in that after calling a method to update a view ex. setText("...") no other methods that follow are called. This was fixed by moving method calls around, but code like this will likely lead to further issues and I would think there would be a better way.

I should also mention that the activity has a launchMode of singleTask to ensure I obtain the instance already running.

<activity android:name=".activities.MyActivity开发者_如何学C" android:launchMode="singleTask" />

Is there a better way I can pass data back to the activity via the notification?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号