问题
I have 2 activities (A and B) and they have 2 buttons to switch between.
- A oncreate
- B oncreate
- A oncreate
- A onresume
what I wanted to do is after sending intent from B to A oncreate should not be called but at this point it does. To overcome that I found FLAG_ACTIVITY_REORDER_TO_FRONT (from here) and thought it could called only onresume but it didn't.
回答1:
FLAG_ACTIVITY_REORDER_TO_FRONT
does exactly what you think it should do. However, if you start ActivityA
and then ActivityA
starts ActivityB
and calls finish()
on itself, then when ActivityB
starts ActivityA
with an Intent that has FLAG_ACTIVITY_REORDER_TO_FRONT
there will be no instance of ActivityA
to bring to the front. In this case Android will simply create a new one. I can only assume that is what you are seeing.
回答2:
FLAG_ACTIVITY_REORDER_TO_FRONT changes activity history. If the requested activity is found in the history of previously visited activities (in a task), the older history record for this activity is cleared. So, while pressing back button, user will not encounter this activity in a task.
This flag won't affect the call to onCreate()
, If activity does not exists in the task (not loaded or destroyed), onCreate()
will be called to create it.
回答3:
You can't just cancel onCreate. If B is full screen activity android can kill A activity and will recreate it when you try to restart it with FLAG_ACTIVITY_REORDER_TO_FRONT flag and call it's onCreate method. If Activity A will be still alive at the monent when you try to bring it to front, onCreate method should not be called.
Maybe in your case you should try to use fragments?
来源:https://stackoverflow.com/questions/15224127/oncreate-called-despite-using-flag-activity-reorder-to-front