简体   繁体   中英

Avoid the physical back button in Android

I want to avoid that the android users play my game and get gameover, that they simply click the back button and come back to the game.

This is my code from jumping of my GameActivity to jump into the new GameOver Activity. What a command could I use? I simply want to come back to the main activity not the gameActivity.

private void checkFails(){
    TextView failsleft = (TextView) findViewById(R.id.fails);
    if(fails < 1){
        failsleft.setText("Fails left: "+fails);
        Intent intent = new Intent(this, GameOverActivity.class);
        startActivity(intent);

    }else{
        failsleft.setText("Fails left: "+fails);
        fails--;
    }
}

You can override the method "OnBackPressed" in Activity and do any custom logic there. Here is a link to the documentation https://developer.android.com/reference/android/app/Activity.html#onBackPressed()

If your GameActivity is started by MainActivity, you can use an extra to tell the main activity that the game is ended. So you need to start the GameActivity with startActivityWithResult .

In MainActivity , start the GameActivity with startActivityForResult() :

public class MainActivity extends AppCompatActivity {

    ...

    // Request code for handling the GameActivity result
    // make it final so it can be changed.
    private static final int gameOverRequestCode = 777;

    // Start GameActivity
    private void startTheGame() {
      Intent intent = new Intent(this, GameActivity.class);
      // start the GameActivity and wait for the result.
      startActivityForResult(intent, gameOverRequestCode);
    }


    @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      switch (requestCode) {

        // Handle the result from GameActivity.
        // when the result is accepted from GameActivity, start the GameOverActivity.
        case gameOverRequestCode:
          if (resultCode == Activity.RESULT_OK) {
            Intent intent = new Intent(this, GameOverActivity.class);
            startActivity(intent);
          }
          break;
      }
    }

    ...
}

In GameActivity , send the result when the game is over:

private void checkFails(){
    TextView failsleft = (TextView) findViewById(R.id.fails);
    if(fails < 1){
        failsleft.setText("Fails left: "+fails);

        // Send the result back to main activity
        Intent i = getIntent();
        // you can add any extra here, for example, game state.
        // i.putExtra("extra_key", value);
        setResult(Activity.RESULT_OK, i);

        // Finish the GameActivity because we don't need it anymore.
        finish();

    }else{
        failsleft.setText("Fails left: "+fails);
        fails--;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM