Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7683065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:42:40+00:00 2026-05-31T18:42:40+00:00

I have come across an issue with my application, I have an option class

  • 0

I have come across an issue with my application, I have an “option” class which has a ToggleButton in it which handles the playback of music (on or off) which I created in a Session. However, if I turn the music off, change screen and go back to the option class, the ToggleButton goes back to “on”, even though the music is no longer playing, which means I have to press the button twice for the music to actually come back on. Does anyone have any ideas?

Service Class:

public class MyMusicService extends Service {

MediaPlayer mp;


@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    mp = MediaPlayer.create(this, R.raw.song);


    mp.start();
    mp.setLooping(true);
    return super.onStartCommand(intent, flags, startId);
}


@Override
public IBinder onBind(Intent intent) {

    return null;
}


@Override
public void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
    if(mp!=null) {
        mp.stop();
        mp.release();
    }
    mp=null;
}

}

Option class:

public class OptionsActivity extends Activity {

Intent i; // Handles MyMusicService.java
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.options);
    //setting the layout

    i=new Intent(this, MyMusicService.class);
    final ToggleButton soundOption = (ToggleButton) findViewById(R.id.soundPref);

    soundOption.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {   


            // Perform action on clicks to control sound being on and off.   
            if (soundOption.isChecked()) {   
                Toast.makeText(OptionsActivity.this, "Music on.", Toast.LENGTH_SHORT).show();  
                startService(i);  
            } else {   
                stopService(i);
                Toast.makeText(OptionsActivity.this, "Music off.", Toast.LENGTH_SHORT).show(); 
            }  


        }});

}

}

MainActvitiy class

Intent i;

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    //setting the layout

    //Auto starts playing music on app launch.
    i = new Intent(this,MyMusicService.class);
    startService(i);

Could it be something to do with the fact in the XML I have:

android:checked="true"

Thanks for any help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T18:42:41+00:00Added an answer on May 31, 2026 at 6:42 pm

    Yes, the ToggleButton will be initialized anew, when you come back to this view, so android:checked="true" comes back into play.

    You can use the method myToggleButton.setChecked(boolean isMusicPlaying) to reset the state of the Button.
    So you have to check whether the music is playing or not, when you come back to this view and set the state of the button accordingly.


    You can use the method as described in another thread to find out if your music service is running.

    private boolean isMyServiceRunning(String serviceCanonicalClassName) {
        ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
        for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
            if (serviceCanonicalClassName.equals(service.service.getClassName())) {
                return true;
            }
        }
        return false;
    }
    

    so you simply call it to find out of your service is running.
    And then, right after finding out where your ToggleButton is in your layout, you can define if it should be toggled or not:

    boolean musicPlays = isMyServiceRunning(MyMusicService.class.getCanonicalName());
    final ToggleButton soundOption = (ToggleButton) findViewById(R.id.soundPref);
    soundOption.setChecked(musicPlays)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've come across this issue in my application, and have reproduced it using Apple's
hey there i wonder if any of you have come across a similar issue?
I’m new to Selenium and I have come across a test case issue in
I have come across a situation (which I think is weird but is possibly
We are developing a web application in Umbraco 4 and have come across an
Sometimes I have come across this issue that whenever I change the order of
I've been working on a web application using Bootstrap, and have come across odd
I have been researching this issue for long. But I haven't come across any
I have recently come across possibly what is the most silly implementation of application
We have come across a scenario where we need to track the setting and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.