android parent activity killed on startActivity

落花浮王杯 提交于 2019-12-10 11:14:23

问题


I have a simple application with a listview. When a user taps on a list item, i start a new activity

Intent eventdetails = new Intent(HomeActivity.this,EventDetailsActivity.class);
eventdetails.putExtra("eventId", ev.getId());
eventdetails.putExtra("eventDate", ev.getEnglishDate());
startActivity(eventdetails);

As soon as the EventDetailsActivity is started, the HomeActivity is destroyed (onDestroy is called). WHen the user hits the back button from EventDetailsActivity, the HomeActivity is recreated which is something i do not want.

I havent been able to figure out why the HomeActivity is killed in the first place. I checked the activity stack using udb as well and it does not show the HomeActivity

Running activities (most recent first):
Run #0: ActivityRecord{43938cc8 com.app.event/.EventDetailsActivity}

This is happening only on Samsung Galaxy Note 2

Any idea why this would be happening ?


回答1:


this will happen when the user has enabled this setting

goto settings->Developer options

in that in APPS category(scroll down to see), see the option
Don't keep Activities (Destroy every Activity as soon as user leaves it).

see this option is selected or not




回答2:


Intent eventdetails = new Intent(HomeActivity.this,EventDetailsActivity.class);
eventdetails.putExtra("eventId", ev.getId());
eventdetails.putExtra("eventDate", ev.getEnglishDate());
eventdetails.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
eventdetails.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(eventdetails);
HomeActivity.this.finish();

try this code let me know if its help you




回答3:


its because android manages the process of release activities,if you want to finish the previous activity,then add this after start activity:

HomeActivity.this.finish();

either you can you use flag to clear previous activities

eventdetails.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
eventdetails.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

or both:

Intent eventdetails = new Intent(HomeActivity.this,EventDetailsActivity.class);
eventdetails.putExtra("eventId", ev.getId());
eventdetails.putExtra("eventDate", ev.getEnglishDate());
eventdetails.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
eventdetails.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(eventdetails);
HomeActivity.this.finish();

hope it will help to resolve the issue.




回答4:


Maybe that'll be useful for someone - in my case the problem was that the parent activity was defined in the manifest with

android:noHistory="true"

With this setting it was killed as soon as the new activity was opened. See How does android:noHistory="true" work?



来源:https://stackoverflow.com/questions/20325335/android-parent-activity-killed-on-startactivity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!