如何为导航设置“返回”选项和当前活动。

huangapple go评论81阅读模式
英文:

How to set On back option for Navigation and Current activity

问题

我创建了一个带有列表视图的应用程序,当我从主页点击返回按钮时,它会弹出一个消息并询问是否要退出。之后,我实现了一个导航抽屉并进行了打开和关闭的设置。但是,当我从导航栏点击返回按钮返回并弹出我设置在主页上的消息时,

@Override
public void onBackPressed() {
    if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
        drawerLayout.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
    
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("您确定要退出吗?")
            .setCancelable(false)
            .setPositiveButton("是的", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    finish();
                    AdminHome.super.onBackPressed();
                }
            })
            .setNegativeButton("否", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });

    AlertDialog alertDialog = builder.create();
    alertDialog.show();
}

如何在我从导航栏点击返回时停止弹出消息,并将消息设置为从主页弹出。

英文:

I have a created a app with List view when i click back button from home page it will toast a message and ask to exit or not. after i have implement a Navigation drawer and set open and close. but when i click back button from navigation go back and popup the toast which i set to hoe page.

@Override
public void onBackPressed() {

    if (drawerLayout.isDrawerOpen(GravityCompat.START)){
        drawerLayout.closeDrawer(GravityCompat.START);
    }else{
        super.onBackPressed();
    }

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("Are you sure you want to Exit?")
            .setCancelable(false)
            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    finish();
                    AdminHome.super.onBackPressed();
                }
            })

            .setNegativeButton("No", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });

    AlertDialog alertDialog = builder.create();
    alertDialog.show();
}

how to stop the toast message when i click back from navigation bar and set toast from home page.

答案1

得分: 0

把 AlertDialog 代码放在 else 块中。

英文:

Put the AlertDialog code in else block.

答案2

得分: 0

    @Override
    public void onBackPressed() {
        if (drawerLayout.isDrawerOpen(GravityCompat.START)){
            drawerLayout.closeDrawer(GravityCompat.START);
        } else {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage("确定要退出吗?")
                    .setCancelable(false)
                    .setPositiveButton("是的", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            finish();
                            AdminHome.super.onBackPressed();
                        }
                    })
                    .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.cancel();
                        }
                    });

            AlertDialog alertDialog = builder.create();
            alertDialog.show();
        }
    }
英文:

hi You can change your code like this

@Override
public void onBackPressed() {

if (drawerLayout.isDrawerOpen(GravityCompat.START)){
    drawerLayout.closeDrawer(GravityCompat.START);
}else{
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to Exit?")
        .setCancelable(false)
        .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                finish();
                AdminHome.super.onBackPressed();
            }
        })

        .setNegativeButton("No", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });

AlertDialog alertDialog = builder.create();
alertDialog.show();
}

}

答案3

得分: 0

把你的警示对话框放在条件的 else 里面:

@Override
public void onBackPressed() {

    if (drawerLayout.isDrawerOpen(GravityCompat.START)){
        drawerLayout.closeDrawer(GravityCompat.START);
    }else{
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("确定要退出吗?")
            .setCancelable(false)
            .setPositiveButton("是", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    super.onBackPressed();
                }
            })

            .setNegativeButton("否", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });

       AlertDialog alertDialog = builder.create();
       alertDialog.show();
    }

}
英文:

Put your alertdialog inside else of your condition

@Override
public void onBackPressed() {

    if (drawerLayout.isDrawerOpen(GravityCompat.START)){
        drawerLayout.closeDrawer(GravityCompat.START);
    }else{
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage("Are you sure you want to Exit?")
            .setCancelable(false)
            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    super.onBackPressed();
                }
            })

            .setNegativeButton("No", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });

           AlertDialog alertDialog = builder.create();
           alertDialog.show();
    }

}

huangapple
  • 本文由 发表于 2020年5月4日 20:23:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/61592156.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定