我正在制作校园地图应用程序。所以我有这个Dialog,它是一个学院的部门列表,例如EnggDeptDialog.class ...如果我从列表中打开一个,例如土木工程系/ CeFacultyListView.class,它将承担该部门的学院列表,然后当我点击后退按钮时,我希望应用程序返回上一个活动,即EnggDeptDialog.class。明白我的观点?
我似乎没有从这里找到答案,所以我问这个。这里有代码,我尝试过,但它没有真正起作用。
以下是我用过的代码..
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
if (item.getItemId() == R.id.menu_legal) {
startActivity(new Intent(this, EnggDean.class));
return true;
}
if (item.getItemId() == R.id.back){
onBackPressed();
}
return super.onOptionsItemSelected(item);
}
public void onBackPressed() {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);
}
但是每次我点按后退按钮,都会从应用程序中消失。
这是我的对话活动代码:
public class EnggDeptDialog extends DialogFragment{
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(R.string.opt)
.setItems(R.array.enggdept_options, new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which) {
switch(which){
case 0:
Intent aefaculty = new Intent("com.android.cmumap.AFLV");
startActivity(aefaculty);
break;
case 1:
Intent cefaculty = new Intent("com.android.cmumap.CFLV");
startActivity(cefaculty);
break;
case 2:
Intent eefaculty = new Intent("com.android.cmumap.EFLV");
startActivity(eefaculty);
break;
case 3:
Intent itfaculty = new Intent("com.android.cmumap.IFLV");
startActivity(itfaculty);
break;
case 4:
Intent mefaculty = new Intent("com.android.cmumap.MFLV");
startActivity(mefaculty);
break;
}
}
});
return builder.create();
}
}
这就是我调用对话活动的方式
EnggiDeptDialog deptdialog;
deptdialog = new EnggDeptDialog();
deptdialog.show(getFragmentManager(), "Departments");
答案 0 :(得分:0)
您需要做的只是删除public void onBackPressed()
功能。
活动会自动添加到“堆栈”,每次用户点击设备上的“后退”按钮时,Android都会从堆栈中弹出最顶层的活动。这是默认行为。
因此无需手动执行此操作。
答案 1 :(得分:0)
以下是关于如何在Android WebView中对后退按钮进行编码的类似问题的答案。
How to go back to previous page if back button is pressed in WebView?