I'm using a singleTop Activity to receive intents from a search-dialog via onNewIntent()
.
What I noticed is that onPause()
is called before onNewIntent()
, and then afterwards it calls onResume()
. Visually:
- search dialog initiated
- search intent fired to activity
onPause()
onNewIntent()
onResume()
The problem is that I have listeners registered in onResume()
that get removed in onPause()
, but they are needed inside of the onNewIntent()
call. Is there a standard way to make those listeners available?
onNewIntent()
is meant as entry point for singleTop activities which already run somewhere else in the stack and therefore can't callonCreate()
. From activities lifecycle point of view it's therefore needed to callonPause()
beforeonNewIntent()
. I suggest you to rewrite your activity to not use these listeners inside ofonNewIntent()
. For example most of the time myonNewIntent()
methods simply looks like this:With all setup logic happening in
onResume()
by utilizinggetIntent()
.Note: Calling a lifecycle method from another one is not a good practice. In below example I tried to achieve that your onNewIntent will be always called irrespective of your Activity type.
OnNewIntent() always get called for singleTop/Task activities except for the first time when activity is created. At that time onCreate is called providing to solution for few queries asked on this thread.
You can invoke onNewIntent always by putting it into onCreate method like