开发者

When to reset a callbackhandler for a Service

i have two Activities A and B and one Service C. A starts B.

A and B:

  • are binding and unbinding to the Service in onStart()/onStop().
  • are implementing a Handler-Object that works with Messages. The Handler is needed, so if C is doing Work and the Result is there, it will give back a result o开发者_运维技巧r message to A or B.
  • are implementing a ServiceConnection-Object. In the Method onServiceConnected from the ServiceConnection-Object, the Handler will be given to the Service C.
  • If i leave A or B, the Handler for the Service will be set to Null in onStop(). This is because the Activity is not active anymore, so it can not use the Result to do something for example showing a Dialog.

I have following Problem:

  • If i start A and use a function from C everything works fine. Resultdialog is showing up.
  • If i start B from A, also everything works fine. Resultdialog is showing up.
  • Now if i press Back to go back to A, then C cannot contact A, cause the handler is still set to null. No Handler created Dialog is showing up.

I think it has to do something with the order of the lifecycle-methodcalls. onStart() for the new Activity is getting called before onStop() from the old activity.


I think you want to use onResume() instead of onStart(), because onStart() is only called when the activity is initially started, or restarted. If B becomes the foreground Activity, hiding A, onPause() will be called for A, and when A becomes the foreground Activity again (because you pressed the back button), onResume() will be called, not onStart() (unless A was deleted because of a low memory situation).

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜