android 返回上一个视图
android go back to previous view
我正在开发一个 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();
}
}
您可以为此使用 Fragment Manager。
在 Fragman Manager 中,您有一个名为的方法:
getBackStackEntryCount()
如果这个 returns 0 你可以关闭你的应用程序,否则你就回去。
在你的activity中:
@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();
}
试试这个,
@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();
}
}
解决方案
@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();
}
}
我正在开发一个 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();
}
}
您可以为此使用 Fragment Manager。
在 Fragman Manager 中,您有一个名为的方法:
getBackStackEntryCount()
如果这个 returns 0 你可以关闭你的应用程序,否则你就回去。
在你的activity中:
@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();
}
试试这个,
@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();
}
}
解决方案
@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();
}
}