如何在主要活动的背面退出应用程序? 我正在使用此代码,但它完成了主要活动,并显示了来自后栈的其他活动。
@Override
public void onBackPressed() {
finish();
}
答案 0 :(得分:7)
我尝试了这个并且它对我的工作非常好,因为我正在开发应用程序,一旦会话过期,用户只能看到少量视图并保持隐藏状态。
@Override
public void onBackPressed() {
moveTaskToBack(true);
}
答案 1 :(得分:4)
您可以使用
public void setListViewHeightBasedOnChildren(StaggeredGridView staggeredGridView, int columns) {
SampleAdapter listAdapter = (SampleAdapter) staggeredGridView.getAdapter();
if (listAdapter == null) {
// pre-condition
return;
}
int totalHeight = 0;
int items = listAdapter.getCount();
int rows = 0;
View listItem = listAdapter.getView(0, null, staggeredGridView);
listItem.measure(0, 0);
totalHeight = listItem.getMeasuredHeight();
float x = 1;
if( items > columns ){
x = items/columns;
rows = (int) (x + 1);
totalHeight *= rows;
}
ViewGroup.LayoutParams params = gridView.getLayoutParams();
params.height = totalHeight;
gridView.setLayoutParams(params);
}
在此活动标签下的清单中
android:noHistory="true"
答案 2 :(得分:3)
你也可以试试这个,
@Override
public void onBackPressed() {
int pid = android.os.Process.myPid();
android.os.Process.killProcess(pid);
}
答案 3 :(得分:2)
试试这个
@override
public void onBackPressed(){
Intent a = new Intent(Intent.ACTION_MAIN);
a.addCategory(Intent.CATEGORY_HOME);
a.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(a);
}
答案 4 :(得分:1)
你可以尝试这个:
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
new AlertDialog.Builder(Activityname.this)
.setTitle("Title")
.setMessage("Do you really want to exit?")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
ActivityCompat.finishAffinity(Activityname.this);
finish();
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
}
希望它能解决你的问题
答案 5 :(得分:1)
Already asked。最受欢迎的答案:
getActivity().finish();
System.exit(0);
答案 6 :(得分:0)
您可以尝试在onBackPressed中添加此代码:
getActivity().finish();
System.exit(0);
答案 7 :(得分:0)
@Override
public void 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 dialogInterface, int i) {
moveTaskToBack(true);
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.cancel();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
答案 8 :(得分:-1)
试试这个,
Intent intent = new Intent(UserProfile.this, Login.class)
.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();
答案 9 :(得分:-1)
试
public void onBackPressed() {
finish();
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
}
使用finish();从一个活动迁移到另一个活动