Android 如何退出应用程序并显示主屏幕?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3226495/
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 09:14:04  来源:igfitidea点击:

How to exit from the application and show the home screen?

androidexitback-button

提问by poojan9118

I have an application where on the home page I have buttons for navigation through the application.

我有一个应用程序,在主页上我有通过应用程序导航的按钮。

On that page I have a button "EXIT" which when clicked should take the user to the home screen on the phone where the application icon is.

在该页面上,我有一个“退出”按钮,单击该按钮后,用户应转到应用程序图标所在的手机主屏幕。

How can I do that?

我怎样才能做到这一点?

回答by ognian

Android's design does not favor exiting an application by choice, but rather manages it by the OS. You can bring up the Home application by its corresponding Intent:

Android 的设计不支持选择退出应用程序,而是由操作系统管理它。您可以通过其相应的 Intent 调出 Home 应用程序:

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

回答by Kartik Domadiya

May be you can try something like this

也许你可以尝试这样的事情

Suppose in our application, we have a number of activities(say ten) and we need to exit directly from this activity. What we can do is, create an intent and go to the root activity and set flag in the intent as

假设在我们的应用程序中,我们有许多活动(比如十个),我们需要直接退出这个活动。我们可以做的是,创建一个意图并转到根活动并将意图中的标志设置为

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

also, add some extra like boolean to the intent

另外,在意图中添加一些额外的布尔值

intent.putExtra("EXIT", true);

Then in root activity, check the value of the booleanand according to that call finish(), in the onCreate()of the root activity

然后在根活动,检查的值boolean,并根据该呼叫结束(),在onCreate()根活动

if (getIntent().getBooleanExtra("EXIT", false)) {
 finish();
}

回答by Ndupza

System.exit(0);

Is probably what you are looking for. It will close the entire application and take you to the home Screen.

可能是您正在寻找的。它将关闭整个应用程序并将您带到主屏幕。

回答by Lazy Ninja

This works well for me.
Close all the previous activities as follows:

这对我很有效。
关闭所有以前的活动,如下所示:

    Intent intent = new Intent(this, MainActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.putExtra("Exit me", true);
    startActivity(intent);
    finish();

Then in MainActivity onCreate() method add this to finish the MainActivity

然后在 MainActivity onCreate() 方法中添加这个来完成 MainActivity

    setContentView(R.layout.main_layout);

    if( getIntent().getBooleanExtra("Exit me", false)){
        finish();
        return; // add this to prevent from doing unnecessary stuffs
    }

回答by Lazy Ninja

first finish your application using method finish();

首先使用方法完成您的应用程序 finish();

and then add below lines in onDestroy for Removing Force close

然后在 onDestroy 中添加以下行以移除强制关闭

android.os.Process.killProcess(android.os.Process.myPid());
super.onDestroy();

回答by Christian

If you want to end an activity you can simply call finish(). It is however bad practice to have an exit button on the screen.

如果你想结束一个活动,你可以简单地调用finish(). 然而,在屏幕上有一个退出按钮是不好的做法。

回答by Buru

Some Activities actually you don't want to open again when back button pressed such Splash Screen Activity, Welcome Screen Activity, Confirmation Windows. Actually you don't need this in activity stack. you can do this using=> open manifest.xml file and add a attribute

有些活动实际上您不想在按下后退按钮时再次打开,例如启动屏幕活动、欢迎屏幕活动、确认窗口。实际上,您不需要在活动堆栈中使用它。您可以使用=> 打开 manifest.xml 文件并添加一个属性

android:noHistory="true"

机器人:noHistory =“真”

to these activities.

到这些活动。

<activity
    android:name="com.example.shoppingapp.AddNewItems"
    android:label="" 
    android:noHistory="true">
</activity>

OR

或者

Sometimes you want close the entire application in certain back button press. Here best practice is open up the home window instead of exiting application. For that you need to override onBackPressed() method. usually this method open up the top activity in the stack.

有时您希望按某些后退按钮关闭整个应用程序。这里的最佳做法是打开主窗口而不是退出应用程序。为此,您需要覆盖 onBackPressed() 方法。通常这种方法会打开堆栈中的顶部活动。

@Override
public void onBackPressed(){
Intent a = new Intent(Intent.ACTION_MAIN);
a.addCategory(Intent.CATEGORY_HOME);
a.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(a);

}

OR

或者

In back button pressed you want to exit that activity and also you also don't want to add this in activity stack. call finish() method inside onBackPressed() method. it will not make close the entire application. it will go for the previous activity in the stack.

在按下后退按钮时,您想退出该活动,而且您也不想将其添加到活动堆栈中。在 onBackPressed() 方法中调用 finish() 方法。它不会关闭整个应用程序。它将用于堆栈中的上一个活动。

@Override
public void onBackPressed() {
  finish();
}

回答by Janusz

It is not recommended to exit your Android Application. See this questionfor more details.

不建议退出您的 Android 应用程序。有关更多详细信息,请参阅此问题

The user can always quit your app through the home button or in the first activity through the back button.

用户始终可以通过主页按钮或在第一个活动中通过后退按钮退出您的应用程序。

回答by trante

(I tried previous answers but they lacks in some points. For example if you don't do a return;after finishing activity, remaining activity code runs. Also you need to edit onCreate with return. If you doesn't run super.onCreate() you will get a runtime error)

(我尝试了以前的答案,但它们在某些方面有所欠缺。例如,如果您return;在完成活动后不做,则剩余的活动代码会运行。此外,您还需要使用 return 编辑 onCreate。如果您不运行 super.onCreate()你会得到一个运行时错误)

Say you have MainActivityand ChildActivity.

假设你有MainActivityChildActivity

Inside ChildActivity add this:

在 ChildActivity 中添加以下内容:

Intent intent = new Intent(ChildActivity.this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("EXIT", true);
startActivity(intent);
return true;

Inside MainActivity's onCreate add this:

在 MainActivity 的 onCreate 中添加以下内容:

@Override
public void onCreate(Bundle savedInstanceState) {

    mContext = getApplicationContext();

    super.onCreate(savedInstanceState);

    if (getIntent().getBooleanExtra("EXIT", false)) {
        finish();
        return;
    }
    // your current codes
    // your current codes
}

回答by sivi

There is another option, to use the FinishAffinity method to close all the tasks in the stack related to the app.

还有另一种选择,使用 FinishAffinity 方法关闭堆栈中与应用程序相关的所有任务。

See: https://stackoverflow.com/a/27765687/1984636

参见:https: //stackoverflow.com/a/27765687/1984636