Android 以编程方式进入主屏幕

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3724509/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:32:27  来源:igfitidea点击:

Going to home screen programmatically

androidhomescreen

提问by Sri Sri

I want to go to the home screen programmatically in Android when the user clicks on button. How can this be done?

当用户单击按钮时,我想在 Android 中以编程方式进入主屏幕。如何才能做到这一点?

回答by Janusz

You can do this through an Intent.

你可以通过一个Intent来做到这一点。

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

This Intent will start the launcher application that the user has defined. Be careful with this because this will look like your application crashed if the user does not expect this.

此 Intent 将启动用户定义的启动器应用程序。小心这一点,因为如果用户不希望发生这种情况,这看起来就像您的应用程序崩溃了。

If you want this to build an exit button from your app please read this article on exit Buttonsin Android

如果您希望它从您的应用程序构建退出按钮,请阅读有关Android 中退出按钮的这篇文章

回答by Jawad Zeb

One line solution

一条线解决方案

moveTaskToBack(true); //activity.moveTaskToBack(true);

it will behave as Home Button is pressed

它将在按下主页按钮时表现

回答by Peter Ajtai

Janusz's answeris great.

Janusz 的回答很好。

The only thing I want to add, which is a little too long for a comment, is that you can go to the home screen without having a reference to the current activity.

我想补充的唯一一件事是,您可以在没有参考当前活动的情况下进入主屏幕,这对于评论来说有点太长了。

Janusz's code needs to be called from an Activity or Fragment due to startActivity(),

由于startActivity(),需要从 Activity 或 Fragment 调用 Janusz 的代码,

To get around this, you can store a static reference to your apps Context in your application file:

要解决此问题,您可以在应用程序文件中存储对应用程序上下文的静态引用:

public class YourApplication extends Application
{

    private static Context mAppContext;

    public void onCreate()
    {
        super.onCreate();
        ...
        YourApplication.mAppContext = getApplicationContext();
    }

    public static Context getContext()
    {
        return mAppContext;
    }

}

Now you can send the user to the device home screen from any class in your app, not just Activities, Fragments, or other Classes with a reference to the current Activity (you can decide whether this is a good or bad thing):

现在,您可以将用户从应用程序中的任何类发送到设备主屏幕,而不仅仅是 Activity、Fragment 或其他引用当前 Activity 的类(您可以决定这是好事还是坏事):

Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
YourApplication.getContext().startActivity(startMain);

回答by st0le

From Android developersite

来自Android 开发者网站

Here are some examples of other operations you can specify as intents using these additional parameters:

以下是您可以使用这些附加参数指定为意图的其他操作的一些示例:

* ACTION_MAIN with category CATEGORY_HOME -- Launch the home screen.

回答by Jim

I know this is a bit late but I also ran into the same problem and here is how I resolved it. Going back to your MainActivityyou need to add flags from the exiting Activity

我知道这有点晚了,但我也遇到了同样的问题,这是我解决的方法。回到MainActivity你需要从退出添加标志Activity

    final Intent mainActivity = new Intent(this, MainActivity.class);
    mainActivity.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    mainActivity.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);

Now when you pressed the back button being MainActivitythe active one, it will go to home screen.

现在,当您按下后退按钮作为MainActivity活动按钮时,它将进入主屏幕。

回答by Tom Taylor

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