简体   繁体   English

如何禁用Android中的先前活动

[英]How to disable previous activity in Android

Sorry for my bad English. 对不起,我的英语不好。

I have two activity. 我有两项活动。 called Act1 and Act2 称为Act1和Act2

Act2 will be started when a Button in Act1 is clicked (startActivity). 单击Act1中的Button(startActivity)时将启动Act2。 My question is how to disabled back button in Act2 so, I cannot return to Act1. 我的问题是如何在Act2中禁用后退按钮,所以,我无法返回到Act1。

Is it possible. 可能吗。 Thank you. 谢谢。

How to overcome this problem : 如何克服这个问题:

Override onKeyDown Method, end Set finished(); 覆盖onKeyDown方法,结束Set finished();

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK)) {
        finish();
    }
    return super.onKeyDown(keyCode, event);
}

It's been asked before, so you'll find plenty of good answers here: 以前有人问过,所以你会在这里找到很多好的答案:

Override back button to act like home button 覆盖后退按钮,就像主页按钮一样

Just override the back key and call finish() instead. 只需覆盖后退键并调用finish() That way it won't go back to the first activity, it'll just close. 这样它就不会回到第一个活动,它就会关闭。

After startActivity(i); 在startActivity(i)之后; call finish() to close previous activity. call finish()关闭之前的活动。 or use Intent.FLAG_ACTIVITY_NO_HISTORY 或使用Intent.FLAG_ACTIVITY_NO_HISTORY

There are several answers. 有几个答案。 Some you can find here: Removing an activity from the history stack 您可以在此处找到一些: 从历史堆栈中删除活动

You can also override the backbutton to do exactly what you want. 您还可以覆盖后退按钮以完全按照您的意愿执行操作。

Basically you need this, 基本上你需要这个,

Intent.FLAG_ACTIVITY_CLEAR_TASK Intent.FLAG_ACTIVITY_CLEAR_TASK

This clears your current activity stack, ie 这会清除您当前的活动堆栈,即

if you have activity A and it starts activity B with this flag pressing the back button will not return you to Activity B because your stack now has only B. 如果您有活动A并且它使用此标志启动活动B,则按后退按钮将不会返回活动B,因为您的堆栈现在只有B.

This method is cleaner and requires less code than overriding the back button. 与覆盖后退按钮相比,此方法更简洁,所需代码更少。

Since it sounds like you want to make it so the back button doesn't go back to Activity 1, but you probably still need a functional back button in Acitity 2 (maybe to deal with a potential Activity 3 or to handle the Fragment Backstack), the simplest thing is to leave the back button functionality as is, and simply wipe Activity 1 from the backstack. 因为听起来你想要这样做,所以后退按钮不会回到活动1,但你可能仍需要在Acitity 2中使用功能后退按钮(可能需要处理潜在的Activity 3或处理片段Backstack)最简单的方法是按原样保留后退按钮功能,只需从后台堆栈中擦除活动1即可。 Do this by calling finish() on Activity 1 as you switch to Activity 2: 通过在切换到活动2时调用活动1上的finish()来执行此操作:

// This is in Activity 1. It is how we switch to Activity 2:
Intent intent = new Intent(this, Activity2.class);      
startActivity(intent);
finish();
// And now Activity 1 cannot be navigated back to with the back button
// but the back button functionality remains intact.

If you leave it like below to I presume it wont crash 如果你把它留在下面,我认为它不会崩溃

@Override 

public boolean onKeyDown(int keyCode, KeyEvent event) { if ((keyCode == KeyEvent.KEYCODE_BACK)) { public boolean onKeyDown(int keyCode,KeyEvent event){if((keyCode == KeyEvent.KEYCODE_BACK)){

} 
return super.onKeyDown(keyCode, event); 

} }

use this code in activiy 2... 在活动中使用此代码2 ...

public boolean onKeyDown(int keyCode, KeyEvent event) {

             switch(keyCode){
             case KeyEvent.KEYCODE_BACK:

             break;
             }
             return false;
             }

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

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