I have one button for play and stop. The buttons work perfectly, but when I play music, go into the background to some other app, then return to my app, my play/stop button is starting to play again if I press it. This is a problem because I need to press the play button again (although the music is playing) and then press the stop button. I find way to do it with onSaveInstanceState() , but it is not working , problem is here again I need to press play button to stop music whitch is playing in background my code:
int button_status=1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.player);
if (savedInstanceState != null) {
button_status = savedInstanceState.getInt("EXTRA_IS_ON");
}}
@Override
public void onSaveInstanceState(Bundle outState) {
outState.putInt("EXTRA_IS_ON", button_status);
super.onSaveInstanceState(outState);
}
public void onClick(View v) {
internetKonekcija = pk.konekcijaPremaInternetu();
if(button_status == 1)//play the service
{
button_status=0;
startService(new Intent(this, Service.class));
}
else//stop the service
{
button_status=1;
stopService(new Intent(this, Service.class));
}