我正在开发一个Android应用程序,我想退出应用程序,当涉及到没有视图可用的状态返回时。在此之前,它应该转到上一次关于背压的观点。我怎么能这样做。
我需要什么
我的代码
@Override
public void onBackPressed() {
DialogInterface.OnClickListener dialogInterface = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
break;
case DialogInterface.BUTTON_NEGATIVE:
}
commonVariable.setSteps(0);
}
};
AlertDialog.Builder builder = new AlertDialog.Builder(
MainActivity.this);
builder.setMessage("Are you sure to exit the app?")
.setPositiveButton("Yes", dialogInterface)
.setNegativeButton("No", dialogInterface).show();
}
}
答案 0 :(得分:1)
试试这个,
@Override
public void onBackPressed(){
if(fragment.getBackStackEntryCount() != 0){
DialogInterface.OnClickListener dialogInterface = new DialogInterface.OnClick....
....
}
else{
new AlertDialog.Builder(this)
.setMessage("Are you sure you want to exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
YourActivity.this.finish();
}
})
.setNegativeButton("No", null)
.show();
}
}
答案 1 :(得分:0)
您可以使用Fragment Manager进行此操作。
在Fragmant Manager中,您有一个名为:
的方法getBackStackEntryCount()
如果此操作返回0,您可以关闭应用程序,否则您只需返回。
答案 2 :(得分:0)
在您的活动中:
@Override
public void onBackPressed() {
Fragment fragment = getCurrentFragment();
boolean isHandled = false;
if(fragment != null && (fragment instanceof BaseFragment)){
isHandled = ((BaseFragment) fragment).onBackPressed();
}
if(!isHandled){
super.onBackPressed();
}
}
private Fragment getCurrentFragment() {
Fragment fragment = null;
FragmentManager fragmentManager = getFragmentManager();
if(fragmentManager != null){
fragment = fragmentManager.findFragmentById(R.id.content_frame);
}
return fragment;
}
在BaseFragment中:
public boolean onBackPressed()
FragmentManager manager = getFragmentManager();
if(manager != null){
int count = manager.getBackStackEntryCount();
if(count > 0) {
manager.popBackStack();
} else {
if(isAdded()){
Toast.makeText(getActivity(), "Are you sure?", Toast.LENGTH_SHORT).show();
showDialog();
}
}
}
return true;
}
...
onPositiveButtonClick(){
getActivity().finish();
}
答案 3 :(得分:0)
<强>解强>
@Override
public void onBackPressed() {
if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
getSupportFragmentManager().popBackStack();
} else {
DialogInterface.OnClickListener dialogInterface = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
break;
case DialogInterface.BUTTON_NEGATIVE:
}
commonVariable.setSteps(0);
}
};
AlertDialog.Builder builder = new AlertDialog.Builder(
MainActivity.this);
builder.setMessage("Are you sure to exit the app?")
.setPositiveButton("Yes", dialogInterface)
.setNegativeButton("No", dialogInterface).show();
}
}