1

I have a MainActivity.java as it follows:

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

    setContentView(R.layout.activity_main);

}

public void startGame(View v){
    
    Intent i = new Intent(MainActivity.this, StartGame.class);

    startActivity(i);

}

And my StartGame class as it follows:

public class StartGame extends AppCompatActivity {

private Game game;
private Handler updateHandler;
public static Sound sound;

public static Activity activity = null;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);

      setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

      sound = new Sound(this);
      game = new Game(this, 3, 0);
      setContentView(game);

      CreateHandler();
      UpdateThread myThread = new UpdateThread(updateHandler);
      myThread.start();

      activity = this;

  }

  private void CreateHandler() {
      updateHandler = new Handler() {
          public void handleMessage(Message msg) {
              game.invalidate();
              game.update();
              super.handleMessage(msg);
          }
      };
   }

   protected void onPause() {
      super.onPause();
      game.pauseGame();
   }

   protected void onResume() {
      super.onResume();
      game.resumeGame();
   }

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

}

My question here is:

I'm in MainActivity.java and I successfully start a new instance of StartGame through a Button (which fires me the game with and onDraw). Now, if the user presses the soft back button (Navigation bar), the activity will close and the MainActivity layout would come up, but I can still hear the sounds of the game running in the background (even if I go to the Home Screen), meaning that the activity is still running.

I've added the onBackPressed method to StartGame.java but it seems not to work.

Anyone has some clues on what's going on? :/

SlimShadys
  • 119
  • 3
  • 17

3 Answers3

2

Don't know this will work or not but try once,

private UpdateThread myThread

@Override
    public void onBackPressed() {
        super.onBackPressed();
        myThread.stop(); // don't know 'myThread' has 'stop()' or not
        Intent intent = new Intent(this, MainActivity.class); 
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        startActivity(intent);
        finish();
    }

Also try

Arup
  • 131
  • 5
  • This didn't work. I can still hear the audio in the background, meaning that the activity is still open. – SlimShadys Dec 17 '20 at 16:01
  • Thank you for your effort. `stop()` was deprecated, unfortunately, so a workaround would be a boolean check as explained below. Thanks anyways, man. – SlimShadys Dec 17 '20 at 16:20
1

If you have updated any of your application to API LEVEL 33, you have seen this error, onBackPressed is deprecated. Here is alterative of onbackpressed

 fun AppCompatActivity.onBackPressed(isEnabled: Boolean, callback: () -> Unit) {
    onBackPressedDispatcher.addCallback(this,
        object : OnBackPressedCallback(isEnabled) {
            override fun handleOnBackPressed() {
                callback()
            }
        })
}

// How To Use it : 
class ExampleActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_example)

        onBackPressed(true) {
            // do what do you want when get back 
        }
    }

}

upgrade your dependency androidx.activity:activity:1.+.+”

Laxmi kant
  • 128
  • 1
  • 11
  • Thanks for your alternative link {https://www.codeblogs.info/onbackpressed-deprecated-what-is} explain very clearly and easy to use for android API level 33 and above. – Milan Sheth Sep 18 '22 at 14:56
  • What is the use case for adding a callback but not enabling it? I've set up a similar extension, but as I cannot see why one would add a disabled callback, I've just hardcoded your "isEnabled"-parameter in the callback-setup. – Leknesh Jan 04 '23 at 07:09
0

You start a thread you never stop. Is that thread also responsible for the audio theme?

Stop your thread in onBackPressed Also you should call finish() in onBackPressed

Grisgram
  • 3,105
  • 3
  • 25
  • 42
  • You're right. Maybe that thread (which keeps the game alive) needs to be stopped. As for the audio, they are just calls to methods like "playHitSound" which play me a sound whenever the ball hits the brick, so not really that relevant. How should I stop the Thread in onBackPressed? Plus, afaik, super.onBackPressed == finish(). – SlimShadys Dec 17 '20 at 16:03
  • Fixed by adding a boolean check into my Thread and setting `myThread.threadSuspended = true;` just after calling `super.onBackPressed()`. Refer [here](https://stackoverflow.com/questions/13311198/android-kill-specific-thread-on-action-bar-event) for more infos. – SlimShadys Dec 17 '20 at 16:19