Zahid H
Zahid H

Reputation: 245

Having two activities with Mainctivity causes the back button to not work properly

I have a MainActivity and I am posting partial code here:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    switch (item.getItemId()) {
    case R.id.action_quit:
        moveTaskToBack(true);
        return true;
    case R.id.action_help:
        //display the help activity
        Intent myIntent = new Intent( this, ShowHelp.class);
        startActivityForResult(myIntent, 0);
        return true;
    case R.id.action_about:
        //display the about window
        //aboutApp();
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}
public void aboutApp() {
    // custom dialog
    final Dialog dialog = new Dialog(this);
    dialog.setContentView(R.layout.about);
    dialog.setTitle("About TollCulator");

    TextView textViewAbout = (TextView) dialog.findViewById(R.id.tvContents);

    textViewAbout.setText("About: ");
    textViewAbout.append("\n\tTollCulator - Toll Calculator");
    textViewAbout.setMovementMethod(new ScrollingMovementMethod());
    Button btnCloseIt = (Button) dialog.findViewById(R.id.btnOk);
    // if button is clicked, close the custom dialog
    btnCloseIt.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            dialog.dismiss();
        }
    });
    ImageView iv1 = (ImageView) dialog.findViewById(R.id.imgFB);
    iv1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Uri uri = Uri.parse("https://www.facebook.com/PagesByZ");); 
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            dialog.cancel();
            startActivity(intent);
        }
    });
    dialog.show();
}

ShowHelp.class partial code here which is an Activity on it's own:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch(item.getItemId()) {
        case 16908332:
            Intent myIntent = new Intent(getApplicationContext(), MainActivity.class);
            startActivityForResult(myIntent, 0);
            //finish();
    }
    return super.onOptionsItemSelected(item);
}
@Override
public void onBackPressed() {
    // do something on back.
    Intent myIntent = new Intent(getApplicationContext(), MainActivity.class);
    startActivityForResult(myIntent, 0);
    //finish();
    return;
}

Now when i choose action_help from the menu it opens the ShowClass Activity and I can press the back button and it brings me back to MainActivity without any problem.

Now If I choose action_about the About Dialog pops up and If I click on the icon within the Dialog the following pops up: enter image description here

At this time If I decide to press BACK, instead of closing the pop and displaying my About Dialog, I am just taken to the Homescreen.

Commenting the finish(); method in ShowClass allows me to click the BACK button from the "Complete action using" But instead of taking me to the MainActivity, it takes me to ShowClass from there I can press the Back button again to come back to MainActivity. So as long as I choose action_help first, pressing back brings me back to the ShowHelp.

When the Complete action using pops up from the About Dialog, I set up a Log, and it's on the onPause() state and if I did not run ShowHelp in the beginning, my App goes directly to onStop() and onDestroy().

Please help me fix the issue.

To test it, I created a demo without the ShowHelp class with just the dialog and By pressing back button, the Complete actiong using dialog closes and then pressing back again closes the About Dialog. So I am not sure what is going on.

Upvotes: 0

Views: 210

Answers (1)

Vikram
Vikram

Reputation: 51581

Declare a global boolean variable chooserIsShowing:

boolean chooserIsShowing;

Set it to true here:

ImageView iv1 = (ImageView) dialog.findViewById(R.id.imgFB);
iv1.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {

        // set the flag
        chooserIsShowing = true;

        Uri uri = Uri.parse("https://www.facebook.com/PagesByZ");); 
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        dialog.cancel();
        startActivity(intent);
    }
});

Change the overriden onBackPressed() method:

@Override
public void onBackPressed() {

    if (!chooserIsShowing) {
        // do something on back.
        Intent myIntent = new Intent(getApplicationContext(), MainActivity.class);
        startActivityForResult(myIntent, 0);
        finish();
    }

    // remove the flag
    chooserIsShowing = false;
    return;
}

Edit 1:

Declare dialog as a global variable:

Dialog dialog;

Change aboutApp():

public void aboutApp() {

    dialog = new Dialog(this);
    ....

}

Override onKeyDown(int, KeyEvent) method in MainActivity:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {

    if (keyCode == KeyEvent.KEYCODE_BACK) {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
            return true;
        }
    }
    return super.onKeyDown(keyCode, event);
}

You can remove chooserIsShowing as its not required.

Final Solution:

A noHistory flag was defined for this activity in the manifest. Removing that and fixing onBackPressed() method of ShowHelp resolved the issue.

Upvotes: 1

Related Questions