简体   繁体   English

从Android活动转到主屏幕

[英]Go to home screen from Android Activity

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. 我正在用android开发一个应用程序,我想实现一个按钮,以便每当按下它时,我都回到我的主屏幕。 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 谢谢

Try this 尝试这个

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

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 http://developer.android.com/design/patterns/navigation.html

The Android architecture is not ready to exit an Application with one line of code. Android体系结构尚不准备使用一行代码退出应用程序。 You just cannot do it. 你就是做不到。 Rely on your hardware buttons or make a finish() waterfall effect on all your Activities . 依靠您的硬件按钮或对所有Activities进行finish()瀑布效果。 You can use startActivityForResult() to start your Activities (all of them if you want this method to work). 您可以使用startActivityForResult()来启动您的活动(如果希望此方法起作用,请全部startActivityForResult() )。 Then, when you want to exit your App, just call setResult(Activity.USER_CANCELED); 然后,当您要退出应用程序时,只需调用setResult(Activity.USER_CANCELED); and finish(); finish(); right after it. 之后。 It will return to your previous Activity onActivityResult() callback. 它将返回到您先前的Activity onActivityResult()回调。 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); 在那里,如果requestcode是正确的,并且resultCode等于Activity.USER_CANCELED ,则执行相同的操作:调用setResult(Activity.USER_CANCELED); and finish(); finish(); . Once more, it will take you back to the previous Activity , if it exists. 再一次,它将带您回到上一个Activity (如果存在)。 And so on, until you exit your app. 依此类推,直到退出应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM