1

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");
myooomyoo
  • 135
  • 2
  • 5
  • 15

2 Answers2

0

What you need to do is just remove the public void onBackPressed() function.

Activies are automatically added to a 'stack' and every time the user hits the 'back' button on the device, Android pops the topmost Activity from the stack. This is the default behavior.

So there's no need to do this manually.

Merlevede
  • 8,140
  • 1
  • 24
  • 39
  • But don't you think it's another story when user hits back and android pops out the dialog activity? because once, I hit the back button but then it pops out to the main marker. The dialog activity didn't popped. – myooomyoo Feb 20 '14 at 03:41
  • @Banananadss I'm sorry, I'm not following you. What is the 'main marker'? – Merlevede Feb 20 '14 at 03:54
  • This is a map app.. I'm pointing out a Map marker.. When I hit the back button, it skips the Dialog Activity and goes back to the Map.. – myooomyoo Feb 20 '14 at 03:57
  • I think I start to get it. So when you hit the back button you want to go back to the Dialog, not the map, correct? If yes, can you share more of your Dialog code? – Merlevede Feb 20 '14 at 04:02
  • Yes yes! that's what I'm trying to say. Ok. I will edit the above question first. – myooomyoo Feb 20 '14 at 04:04
  • Done editing.. I have put on the Dialog Activity code. – myooomyoo Feb 20 '14 at 04:08
  • @Banananadss The weird thing is, I don't see anywhere in your code where the dialog is being dismissed. When you hit the back button it should still be there. – Merlevede Feb 20 '14 at 04:28
  • I put the code on how I call a dialog activity... @Merlevede I dunno how to put it on the back button. – myooomyoo Feb 20 '14 at 04:34
0

Here's the answer to a similar question about how to code the back button in Android WebView.

How to go back to previous page if back button is pressed in WebView?

Community
  • 1
  • 1
Matt Smith
  • 1,932
  • 1
  • 21
  • 41