简体   繁体   English

在android中关闭硬件后退按钮上的应用程序

[英]closing application on hardware back button in android

I have seen a bunch of posts like this, but none of the solutions has worked for me.我看过一堆这样的帖子,但没有一个解决方案对我有用。 I have an app, where the first thing that is loaded is the LoginActivity, it checks to see if you are already logged in, if you are it send you to the MainActivity, which is the main meat of the application.我有一个应用程序,其中加载的第一件事是 LoginActivity,它检查您是否已经登录,如果您已经登录,它会将您发送到 MainActivity,它是应用程序的主要内容。 My problem is, when you are on the MainActivity and you hit the hardware back button, it just reloads the MainActivity because, I'm assuming it tries to go "back" to the LoginActivity.我的问题是,当您在 MainActivity 上并按下硬件后退按钮时,它只会重新加载 MainActivity,因为我假设它尝试“返回”到 LoginActivity。

I have tried various "fixes" from posts I have seen on here, but none of them seem to work.我在这里看到的帖子中尝试了各种“修复”,但它们似乎都不起作用。 Currently in the AndroidManifest.xml file for my MainActivity I have added the目前在我的 MainActivity 的 AndroidManifest.xml 文件中,我添加了

android:noHistory="true"机器人:noHistory =“真”

To the MainActivity intent, but that isn't making much of a difference.对于 MainActivity 意图,但这并没有太大区别。 I have seen another post where someone mentioned basically starting a new Activity with a special Intent that makes the system load your home screen, but someone pointed out that if someone loads your app, hits back, then does it again it keeps adding to the "stack" and that can be very bad.我看过另一篇文章,其中有人提到基本上启动一个带有特殊意图的新活动,使系统加载您的主屏幕,但有人指出,如果有人加载您的应用程序,回击,然后再次执行,它会不断添加到“堆栈”,这可能非常糟糕。

So I am hoping someone has encountered something similar, where you have to have the LoginActivity load first to verify the user, then that launches Main, but then you need to kill the app on back button press from the MainActivity.所以我希望有人遇到过类似的事情,你必须先加载 LoginActivity 来验证用户,然后启动 Main,但是你需要从 MainActivity 按下后退按钮来终止应用程序。

Thank you for any help in advance.提前感谢您的任何帮助。 I appreciate it.我很欣赏。

Create a function onActivityResult to your LoginActivity and call the MainActivity using the startActivityForResult()为您的LoginActivity创建一个onActivityResult函数并使用startActivityForResult()调用MainActivity

EXAMPLE:示例:

LoginActivity:登录活动:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    startActivityForResult(new Intent(this, MainActivity.class), 1);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
        if (data.getBooleanExtra("EXIT", false)) {
            finish();
        }
    }
}

To close the entire app from MainActivity use the onBackPressed .要从MainActivity关闭整个应用程序,请使用onBackPressed don't forget to remove the super.onBackPressed()不要忘记删除super.onBackPressed()

MainActivity:主要活动:

@Override
public void onBackPressed() {
    // super.onBackPressed();
    Intent intent = new Intent();
    intent.putExtra("EXIT", true);
    setResult(RESULT_OK, intent);
    finish();
}

I have an app that the first screen is a splash activity(shows my logo) and after some seconds it goes to MainActivity.我有一个应用程序,它的第一个屏幕是一个启动活动(显示我的徽标),几秒钟后它转到 MainActivity。 In your LoginActivity close your LoginActivity after starting your MainActivity.在您的 LoginActivity 启动 MainActivity 后关闭您的 LoginActivity。 do something like this:做这样的事情:

     Intent mainIntent = new Intent(LoginActivity.this,MainActivity.class);
     LoginActivity.this.startActivity(mainIntent);
     LoginActivity.this.finish();

When I have this kind of issue, I'm adding in "AndroidManifest.xml" for every Activity this :当我遇到这种问题时,我会为每个活动添加“AndroidManifest.xml”:

android:launchMode="singleTask"

In your loginActivity :在您的 loginActivity 中:

startActivity(new Intent(this, MainActivity.class));
finish();

Check this : https://developer.android.com/guide/topics/manifest/activity-element.html检查这个: https : //developer.android.com/guide/topics/manifest/activity-element.html

In contrast, "singleTask" and "singleInstance" activities can only begin a task.相比之下,“singleTask”和“singleInstance”活动只能开始一个任务。 They are always at the root of the activity stack.它们始终位于活动堆栈的根部。 Moreover, the device can hold only one instance of the activity at a time — only one such task.此外,设备一次只能保存一个活动实例——只有一项这样的任务。

You can try my code :你可以试试我的代码:

void gotoLogin() {
    Intent t = new Intent(SplashScreenActivity.this, LoginActivity.class);
    finish();
    startActivity(t);
}

and

void gotoMainActivity() {
    Intent t = new Intent(SplashScreenActivity.this, MainActivity.class);
    finish();
    startActivity(t);
}

after you check检查后

if ( you logged in){
   gotoMainActivity();
} else {
   gotoLogin();
}

I hope it can help your problem!我希望它可以帮助您解决问题! Thank you!谢谢!

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

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