以编程方式退出android应用程序

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

exit android application programmatically

androidexit

提问by choco

I have two activities, the first is a splash activity. I would like to know how to exit the application from the second activity to the homepage. I've used this method it works BUT it takes to the launcher.

我有两个活动,第一个是飞溅活动。我想知道如何将应用程序从第二个活动退出到主页。我已经使用了这种方法,但它需要启动器。

public void AppExit() 
{

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


}

回答by Sripathi

Whenever you wish to exit all open activities, you should press a button which loads the first Activity that runs when your application starts then clear all the other activities, then have the last remaining activity finish. to do so apply the following code in ur project

每当您希望退出所有打开的活动时,您应该按下一个按钮,加载应用程序启动时运行的第一个活动,然后清除所有其他活动,然后完成最后一个剩余活动。为此,请在您的项目中应用以下代码

Intent intent = new Intent(getApplicationContext(), FirstActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("EXIT", true);
startActivity(intent);

The above code finishes all the activities except for FirstActivity. Then we need to finish the FirstActivity's Enter the below code in Firstactivity's oncreate

以上代码完成了除FirstActivity之外的所有活动。然后我们需要完成FirstActivity的在FirstActivity的oncreate中输入以下代码

if (getIntent().getExtras() != null && getIntent().getExtras().getBoolean("EXIT", false)) {
    finish();
}

and you are done....

你已经完成了......

From Finish all activities at a time

从一次完成所有活动

回答by elfekz

when you want to close your application, you can call

当你想关闭你的应用程序时,你可以调用

finishAffinity();

完成亲和力();

or if you want close it in background also you should write,

或者如果你想在后台关闭它,你也应该写,

android:excludeFromRecents="true"

android:excludeFromRecents="true"

in AndroidManifest :

在 AndroidManifest 中:

       <activity
        android:name="com.smart.remote.main.SplashActivity"
        android:configChanges="orientation|keyboardHidden"
        android:screenOrientation="portrait" 
        android:excludeFromRecents="true">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

回答by Emm Jay

To finish an activity or exit the app, try this..

要完成一项活动或退出应用程序,试试这个..

public void exitApp(View v)
{

finish();

}

& use this if for whatever you select to exit the app.

&如果您选择退出应用程序,请使用它。

android:onClick="exitApp"

android:onClick="exitApp"

回答by BinaryShrub

This is the cleanest way I have come across:

这是我遇到的最干净的方式:

moveTaskToBack(true);
finish();