如何通过挂起的意图将数据从服务传递回暂停的活动?
我有一个应用程序正在检查服务中设备的位置以及它是否已进入许多预定义区域之一。当活动打开时,服务会引用回活动;当活动放入后台时,onPause 会将服务对活动的引用设置为 null。当活动引用为空时,会向活动发送带有悬而未决意图的通知。
我遇到的问题是,当单击通知项后重新打开活动时,不会调用 onCreate 或 onRestoreInstanceState 方法,并且调用的 onResume 没有对添加到的区域对象的引用待处理的意图。
我可以通过在服务中添加一个标志来指示是否附加活动而不是将活动设置为空来使事情正常运行。这允许调用一个活动方法来更新视图,指示已进入定义的区域,但此方法似乎在调用更新视图 ex 的方法后存在问题。 setText("...") 不会调用后面的其他方法。这是通过移动方法调用来解决的,但是这样的代码可能会导致进一步的问题,我认为会有更好的方法。
我还应该提到该活动的 launchMode 为 singleTask 以确保我获得已在运行的实例。
<activity android:name=".activities.MyActivity" android:launchMode="singleTask" />
有没有更好的方法可以通过通知将数据传回活动?
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" android:launchMode="singleTask" />
Is there a better way I can pass data back to the activity via the notification?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论