I have really hard time with background music in my app. I just want to play music in all activities - when I press home button I want to stop music. I want "stop or play" music button in all activities, but couldnĀ“t make it work.
So I decided to make embarassing choice - play it only in 1 activitiy by
onCreate
backgroundmusic = MediaPlayer.create(StoryActivity.this, R.raw.creepy_music);
backgroundmusic.start();
onPause
@Override
protected void onPause() {
super.onPause();
backgroundmusic.release();
finish();
}
Can you please help me with easy activity lifecycle? So when a user presses home button - music will stop. When he will come back to app - music will be restored and this activity too (it is not MainActivity)
Thank you, guys
Here are the different LifeCycle states. Now to your answer,
In
public class MainActivity extends AppCompatActivity
, It's the AppCompatActivity that is the main source of an Activity's functionality, hence in the above methods likesuper.onResume();
andsuper.onStop();
super refers to theAppCompatActivity class