I have an android app where in my Main activity I can play music onCreate(), pause music onPause() and restart music onResume(). I am using MediaPlayer. The problem is that I don’t want the music to restart onResume() when I’m rotating the screen on my Main activity. I only want the music to restart when I’m coming back to my Main activity from another activity. Any suggestions?
private MediaPlayer mp;
mp = MediaPlayer.create(MainActivity.this, R.raw.always_sunny);
mp.setLooping(true);
mp.start();@Override protected void onPause() { super.onPause(); mp.getCurrentPosition(); mp.pause(); } @Override protected void onResume() { super.onResume(); mp.seekTo(0); mp.start(); }
Add the following attribute to the activity in the manifest:
This would avoid that the activity restarts on orientation changes or when the keyboard appears.
If you need to do something when the rotation changes (for example, change an image in your case), add the following method to your activity: