Android: How to make launcher always open the main activity instead of child activity? (or otherwise)

旧时模样 提交于 2019-12-20 10:43:31

问题


I have activities A and B. The A is the one with LAUNCHER intent-filter (i.e. the activity that is started when we click the app icon on home screen).

A launches B using startActivity(new Intent(A.this, B.class)).

When the user has the B activity open, and then put my application into the background, and later my application's process is killed, when the user starts my application again, B is opened instead of A.

This caused a force close in my app, because A is the activity that initializes the resources my app needs, and when B tried to access the uninitialized resources, B crashes.

Do you have any suggestions what should I do in this situation?


回答1:


Well you should really have activity B initialize your resources too. But you can put android:clearTaskOnLaunch="true" in your manifest for activity A to have the launcher always go to that activity.




回答2:


Have you tried setting Flag FLAG_ACTIVITY_NEW_TASK while creating intent. In your case please try startActivity(new Intent(A.this, B.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK))



来源:https://stackoverflow.com/questions/2909893/android-how-to-make-launcher-always-open-the-main-activity-instead-of-child-act

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