Android dev: MainActivity's onBackPressed method uses other activitiy's onBackPressed method, not the default. What is wrong with my code?

StackOverflow https://stackoverflow.com/questions/23335723

  •  10-07-2023
  •  | 
  •  

Question

I have three java activities: MainActivity being the first screen when you open the app. The other two are PlayActivity and GuessActivity I override the onBackPressed() method in both Play and Guess activites: this is the code:

in PlayAcitvity:

    @Override
public void onBackPressed() {
    final Dialog mainquestion = new Dialog(PlayActivity.this);
    mainquestion.setContentView(R.layout.dialog_mainmenu);
    mainquestion.setCanceledOnTouchOutside(false);

    Button yes = (Button) mainquestion.findViewById(R.id.yes);
    Button no = (Button) mainquestion.findViewById(R.id.no);

    yes.setOnClickListener(new OnClickListener() {

          @Override
          public void onClick(View v) {
                Intent backtomain = new Intent(PlayActivity.this, MainActivity.class);
                PlayActivity.this.startActivity(backtomain);
           }
    });

    no.setOnClickListener(new OnClickListener() {

          @Override
          public void onClick(View v) {
                  mainquestion.dismiss();
           }
    });


    mainquestion.show();
}

in GuessActivity:

@Override
public void onBackPressed() {
    final Dialog mainquestion = new Dialog(GuessActivity.this);
    mainquestion.setContentView(R.layout.dialog_mainmenu);
    mainquestion.setCanceledOnTouchOutside(false);

    Button yes = (Button) mainquestion.findViewById(R.id.yes);
    Button no = (Button) mainquestion.findViewById(R.id.no);

    yes.setOnClickListener(new OnClickListener() {

          @Override
          public void onClick(View v) {
                Intent backtomain = new Intent(GuessActivity.this, MainActivity.class);
                GuessActivity.this.startActivity(backtomain);
           }
    });

    no.setOnClickListener(new OnClickListener() {

          @Override
          public void onClick(View v) {
                  mainquestion.dismiss();
           }
    });


    mainquestion.show();
}

in MainActivity I put this:

public void onBackPressed() {
    super.onBackPressed();
}

My goal is when I tap the back button and I'm in the Play or Guess Activity, a dialog box will appear. I have no problem with this, it's working well. My problem is when I'm on the first screen, when I tap the back button it goes to the PlayActivity and the PlayActivity's dialog box appears. I just want the onBackPressed method in MainActivity to work as default.

Was it helpful?

Solution 2

found a better solution:

    Intent i=new Intent(this, MainActivity.class);
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(i);

need to use addFlags.

OTHER TIPS

instead of start another activity in yes button on GuessActivity and PlayAcitvity just call finish() or super.onBackPressed(); method

so your code must be:

yes.setOnClickListener(new OnClickListener() {

          @Override
          public void onClick(View v) {
               YourDialog.dismiss();
               super.onBackPressed();  // or finish()
           }
    });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top