安卓:退出应用程序时,preSS后退按钮 [英] Android: Quit application when press back button

查看:247
本文介绍了安卓:退出应用程序时,preSS后退按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序,我想从应用程序退出时,preSS后退按钮,这是我的code:

  @覆盖
    公共无效onBack pressed(){
        新AlertDialog.Builder(本).setIcon(android.R.drawable.ic_dialog_alert).setTitle(退出)
                .setMessage(你确定要退出吗?)
                .setPositiveButton(是,新DialogInterface.OnClickListener(){
                    @覆盖
                    公共无效的onClick(DialogInterface对话,诠释它){
                        完();
                    }
                })setNegativeButton(否,空).show()。
    }
 

这是正常工作,但是当我从应用程序退出并不完全退出,并显示空白页面,我的应用程序的标志,当我再次preSS后退按钮退出应用程序,该如何解决呢???

编辑:

我完全使用code,而不是上面的,但我的应用程序退出,但我希望它运行在后台并没有完全退出,我该怎么办呢?

  @覆盖
    公共无效onBack pressed(){
        新AlertDialog.Builder(本).setIcon(android.R.drawable.ic_dialog_alert).setTitle(退出)
                .setMessage(你确定吗?)
                .setPositiveButton(是,新DialogInterface.OnClickListener(){
                    @覆盖
                    公共无效的onClick(DialogInterface对话,诠释它){

                        意向意图=新的意图(Intent.ACTION_MAIN);
                        intent.addCategory(Intent.CATEGORY_HOME);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(意向);
                        完();
                    }
                })setNegativeButton(否,空).show()。
    }
 

解决方案

在preSS回来,然后你完成当前的活动(比如A),您将看到您的应用程序的标志空白的活动(比如B)这只是意味着它是一个整理后显示,b活动仍处于backstack,也活性的从b活动开始,所以在活动中,你应该开始用旗活动A作为

 意图launchNextActivity;
launchNextActivity =新的意图(B.class,的A.class);
launchNextActivity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
launchNextActivity.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
launchNextActivity.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(launchNextActivity);
 

现在你的活动,A是最高的堆栈你对backstack应用程序的任何其他活动。

现在在你想实现onBack pressed关闭应用程序的活动时,你可能会做这样的事情,

 私人布尔退出= FALSE;
@覆盖
    公共无效onBack pressed(){
        如果(出口){
            完(); //完成活动
        } 其他 {
            Toast.makeText(这一点,$ P $返回再次PSS退出。
                    Toast.LENGTH_SHORT).show();
            退出= TRUE;
            新的处理程序()。postDelayed(新的Runnable(){
                @覆盖
                公共无效的run(){
                    退出= FALSE;
                }
            },3 * 1000);

        }

    }
 

该处理器在这里处理意外回到presses,它只是显示了吐司,如果有3秒钟内又回到preSS,它将关闭应用程序。

In my application i want exit from app when press back button, this my code:

@Override
    public void onBackPressed() {
        new AlertDialog.Builder(this).setIcon(android.R.drawable.ic_dialog_alert).setTitle("Exit")
                .setMessage("Are you sure you want to exit?")
                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        finish();
                    }
                }).setNegativeButton("No", null).show();
    }

it's work correctly but when i exit from app it does not exit completely and show empty page with my app logo and when i again press back button exit from app, How can i fix it???

EDIT :

I use this code instead of above but my app exit completely but i want it running at background and does not exit completely , how can i do it?

@Override
    public void onBackPressed() {
        new AlertDialog.Builder(this).setIcon(android.R.drawable.ic_dialog_alert).setTitle("Exit")
                .setMessage("Are you sure?")
                .setPositiveButton("yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        Intent intent = new Intent(Intent.ACTION_MAIN);
                        intent.addCategory(Intent.CATEGORY_HOME);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(intent);
                        finish();
                    }
                }).setNegativeButton("no", null).show();
    } 

解决方案

When you press back and then you finish your current activity(say A), you see a blank activity with your app logo(say B), this simply means that activity B which is shown after finishing A is still in backstack, and also activity A was started from activity B, so in activity, You should start activity A with flags as

Intent launchNextActivity;
launchNextActivity = new Intent(B.class, A.class);
launchNextActivity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
launchNextActivity.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);                  
launchNextActivity.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(launchNextActivity);

Now your activity A is top on stack with no other activities of your application on the backstack.

Now in the activity A where you want to implement onBackPressed to close the app, you may do something like this,

private Boolean exit = false;
@Override
    public void onBackPressed() {
        if (exit) {
            finish(); // finish activity
        } else {
            Toast.makeText(this, "Press Back again to Exit.",
                    Toast.LENGTH_SHORT).show();
            exit = true;
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    exit = false;
                }
            }, 3 * 1000);

        }

    }

The Handler here handles accidental back presses, it simply shows a Toast, and if there is another back press within 3 seconds, it closes the application.

这篇关于安卓:退出应用程序时,preSS后退按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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