Broadwell
Broadwell

Reputation: 1384

Disable back button when changing Intent

I have a game that changes activities using intents during the gameplay. The problem is, you can press back after changing to a new activity with the Intent. Is there any way to disable that?

Upvotes: 0

Views: 194

Answers (1)

tiguchi
tiguchi

Reputation: 5410

You can override your activity's onBackPressed method to implement the back button behavior you need. A crude example would be:

class MyActivity extends Activity {
    boolean suppressBackButton;

    public void openOtherActivityMethod() {
        suppressBackButton = true;
        Intent i = new Intent(this, ActivityB.class);
        startActivity(i);
    }

    public void onBackPressed() {
        if(!suppressBackButton) finish();
    }

    protected void onResume() {
        suppressBackButton = false;
    }
}

Upvotes: 1

Related Questions