I am making a Campus Map App. So I have this Dialog, which is a list of Departments of a College, e.g. EnggDeptDialog.class... If I open one from the List, e.g. Civil Engineering Department/ CeFacultyListView.class, it would bear the List of Faculties of that Department, then when I hit the back button I want the app to go back to the previous activity which is the EnggDeptDialog.class. Got my point?
I didn't seem to find answers from here so I'm asking this. There were codes from here that I tried but it didn't really worked.
Below is a code that I used..
@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);
}
But every time i tapped the back button, it goes out from the application.
This is my dialog activity code:
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();
}
}
This is how I call a dialog activity
EnggiDeptDialog deptdialog;
deptdialog = new EnggDeptDialog();
deptdialog.show(getFragmentManager(), "Departments");