Finish child activity when parent activity stops
There is one activity in my program "A1". In A1 onCreate i use intent to call android's activity ("A2"):
Intent contactPickerInt开发者_如何学编程ent = new Intent(Intent.ACTION_PICK,
ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
startActivityForResult(contactPickerIntent, 1010);
when user picks a contact i want to use contact's information in A1. Everything is ok, but...
When i use "home button" in A2, A1 and A2 stil in memory, and if OS kill A1 (or i can do it from task manager), A2 is stil running and when i start my program i see an OLD A2, and when i pick a contact i cant see result in A1, because NEW A1 is started and it calls for NEW A2.
The question is: how can i kill A2, when somebody kills A1 from memory.
You can just call finishActivity(CHILD_ACTIVITY_REQUEST_CALL) in the onStop() method of your parent activity.
In your case:
public void onStop(){
finishActivity(1010);
}
I do not think what you ask is possible.
My bad, as pointed out by nbarraille this is possible. What I say below though, on how one should work with the Android still goes.
More importantly however, is that this is not how you're supposed to do things when working with the Android system. If you read a bit about activities, and in particular how the Activity Lifecycle works, you should get some understanding of the basics. In short, Android takes all the responsibility for "killing" activities when they are no longer needed (and other running activities need the resources they hold).
From what you say in your question, it is not easy to say what causes the problem when you try to call A2 the second time you launch A1, but I feel pretty certain that it doesn't have anything to do that you yourself didn't kill A2.
精彩评论