Go to home screen from Android Activity

自闭症网瘾萝莉.ら 提交于 2019-12-08 00:37:12

问题


I'm making an application in android and I want to implement a button such that whenever it is pressed, I just reach back to my home screen. I know that we have the hardware key and soft keys( when there are no hardware keys) which implements this, but I want to add this functionality for this application. Does anybody has any idea how to do it?

Thank you


回答1:


Try this

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);



回答2:


You can use an icon on your action bar and program it to bring you back to your application home screen.

Take a look at this section of the developers guide. http://developer.android.com/design/patterns/navigation.html




回答3:


The Android architecture is not ready to exit an Application with one line of code. You just cannot do it. Rely on your hardware buttons or make a finish() waterfall effect on all your Activities. You can use startActivityForResult() to start your Activities (all of them if you want this method to work). Then, when you want to exit your App, just call setResult(Activity.USER_CANCELED); and finish(); right after it. It will return to your previous Activity onActivityResult() callback. There, if the requestcode is the right one and the resultCode is equal to Activity.USER_CANCELED, just do the same: Call setResult(Activity.USER_CANCELED); and finish();. Once more, it will take you back to the previous Activity, if it exists. And so on, until you exit your app.



来源:https://stackoverflow.com/questions/25754554/go-to-home-screen-from-android-activity

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