A template for an activity that triggers another action (for the result)

I have an action in which the first thing he does is launch another action for the result. when the result returns, he must process it and then finish. I have a call to startActivityForResult() in onCreate() .

I see that sometimes, when I return from the target activity that I started, onCreate() in my activity is called again. this, of course, restarts the target activity a second time.

It makes sense, and I understand why it is, but I do not understand the correct model of what I'm trying to achieve. When I return from the activity that I started, I do not want to restart the target activity again ... I just want to start onActivityResult() and complete.

I read where someone suggested setting a preference for a state, but this seems like a good source of errors, for example if it is stuck in an incorrect state.

Any thoughts?

+4
source share
1 answer

following here with the solution. the key is to run the target activity in onResume (), not onCreate (). from javadocs to onActivityResult (),

You will receive this call immediately before onResume () when your activity restarts.

In other words, you can be sure that onActivityResult () is called before onResume () ... therefore, for example, set the flag that says "do not start the target activity this time" in onActivityResult (), so when onResume (), you can avoid restarting the target activity.

+2
source

Source: https://habr.com/ru/post/1338288/


All Articles