我想结束整个应用程序,当用户单击后退按钮在安卓 [英] I want to end the whole application when user click back button in android

查看:113
本文介绍了我想结束整个应用程序,当用户单击后退按钮在安卓的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想结束整个应用程序,当用户单击后退按钮android.Currently它再去previous开业活动。 我也尝试重写onBack pressed(),但它不工作。

 公共布尔的onkeydown(INT键code,KeyEvent的事件){
    如果(键code == KeyEvent.KEY code_BACK){
        onBack pressed();
    }
       返回super.onKeyDown(键code,事件);
}
在这里输入code
@覆盖
公共无效onBack pressed(){
    // TODO自动生成方法存根
    super.onBack pressed();
    返回;
}
 

解决方案

试试这个,开始从一个活动,将作为您的根你的应用程序。

 公共类根系延伸活动{

     公共无效的onCreate(){
         super.onCreate();
         handleIntent();
      }

      公共无效onNewIntent(意向意图){
        setIntent(意向);
        handleIntent();
      }

     私人无效handleIntent(){
         布尔结束= getIntent.getBooleanExtra(结束);
         如果(完){
            完();
         } 其他 {
            意向意图=新的意图(这一点,MyRealFirstActivity.class); //其中,这是你希望用户看到,当他们启动应用程序的类。
            startActivity(意向);
         }
      }



      公共无效onResume(){
        super.onResume();
        完();
      }
}
 

然后选择要后退按钮结束应用程序的活动,里面做到以下几点:

 公共类LastActivity延伸活动{

    公共无效onBack pressed(){
        意向意图=新的意图(这一点,Root.class);
        intent.putExtra(结束,真正的);
        intent.addFlag(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(意向);
    }
}
 

这样所有人都开始在你启动应用程序,然后点击Back按钮之间的活动,将全部完成()为您服务。这实质上是将清理自启动应用程序的石板完成,然后退出。

I want to end the whole application when user click back button in android.Currently It again go to previous opened activity. I also try override onBackPressed() but it doesnt work.

public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        onBackPressed();
    }
       return super.onKeyDown(keyCode, event);
}
enter code here
@Override
public void onBackPressed() {
    // TODO Auto-generated method stub
    super.onBackPressed();
    return;
}

解决方案

Try this, start your application from a Activity that will act as your Root.

public class Root extends Activity {

     public void onCreate() {
         super.onCreate();
         handleIntent();
      }

      public void onNewIntent(Intent intent) {
        setIntent(intent);
        handleIntent();
      }

     private void handleIntent() {
         boolean end = getIntent.getBooleanExtra("End");
         if (end) {
            finish();
         } else {
            Intent intent = new Intent(this, MyRealFirstActivity.class); // where this is the class that you want the user to see when they launch your app.
            startActivity(intent);
         }
      }



      public void onResume() {
        super.onResume();
        finish();
      }
}

Then inside the activity that you want the back button to end the app, do the following:

public class LastActivity extends Activity {

    public void onBackPressed() {
        Intent intent = new Intent(this, Root.class);
        intent.putExtra("End", true);
        intent.addFlag(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
    }
}

This way all the activities that were started in between you launching the app and then hitting the back button, will all be finished() for you. This essentially will clean the slate of the app from start to finish and then exit.

这篇关于我想结束整个应用程序,当用户单击后退按钮在安卓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆