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 7697989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T22:08:19+00:00 2026-05-31T22:08:19+00:00

I am making a game and I have an options page which turns the

  • 0

I am making a game and I have an options page which turns the music on or off. I want to override the backbutton so that it goes back to the home page, you’ll understand why when you see my code:

public class OptionsActivity extends Activity {

    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;
    }

    Intent i; // Handles MyMusicService.java


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.options);


        final TextView tSound = (TextView) findViewById(R.id.textView2);

        final Button saveBtn = (Button) findViewById(R.id.optSaveBtn);
        final Button tblBtn = (Button) findViewById(R.id.tableBtn);

        i=new Intent(this, MyMusicService.class);

        final ToggleButton soundOption = (ToggleButton) findViewById(R.id.soundPref);


        boolean musicPlays = isMyServiceRunning(MyMusicService.class.getCanonicalName());


        soundOption.setChecked(musicPlays); 


        if(musicPlays==true){

            tSound.setText("On");
        }

        if(musicPlays==false) { 

            tSound.setText("Off");
        }


        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);
                    Intent i = new Intent(OptionsActivity.this, OptionsActivity.class);
                    startActivity(i);

                } 

                else {  

                    if(stopService(i)==true){

                        soundOption.setChecked(false);
                        stopService(i);
                        Toast.makeText(OptionsActivity.this, "Music off.", Toast.LENGTH_SHORT).show();
                        Intent i = new Intent(OptionsActivity.this, OptionsActivity.class);
                        startActivity(i);

                    }  
                }
            }

        });



        tblBtn.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                Intent tblView = new Intent(OptionsActivity.this, SQLView.class);
                startActivity(tblView);

            }
        });



        saveBtn.setOnClickListener(new View.OnClickListener(){

            public void onClick(View v) {



                switch (v.getId()){ 

                case R.id.optSaveBtn: //Determine what will happen when the user presses the "Submit button".
                    boolean optionsWork = true;
                    try{

                        String sound = tSound.getText().toString();

                        optionsDB entry = new optionsDB(OptionsActivity.this); //Creating a new instance of MasterMind game
                        entry.open();
                        entry.createOptionEntry(sound); //Passing both strings
                        entry.close();

                    }catch (Exception e){ //Creating an error message if for some reason the app cannot transfer data to the Database.

                        Toast.makeText(OptionsActivity.this, "Error", Toast.LENGTH_SHORT).show();
                    }

                    finally { //Creating an AlertDialog box when the user presses the Submit button.

                        if (optionsWork){

                            Toast.makeText(OptionsActivity.this, "Settings Saved", Toast.LENGTH_SHORT).show();

                        }

                    }

                    break;

                }
            }
        });
    }
}

Any help would be fantastic thank you.

Update: Added this, still doesn’t work:

    @Override
public boolean onKeyDown(int keyCode, KeyEvent event)  {
    if (keyCode == KeyEvent.KEYCODE_BACK ) {
        // do something on back.
        return true;
    }
    Intent i = new Intent(OptionsActivity.this, MainActivity.class);
    startActivity(i);
    return super.onKeyDown(keyCode, event);
}
  • 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-31T22:08:20+00:00Added an answer on May 31, 2026 at 10:08 pm

    Override onKeyDown() of Activity, here you can handle various keys or can Override onBackPresses() too

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)  {
        if (keyCode == KeyEvent.KEYCODE_BACK ) {
            // do something on back.
            return true;
        }
    
        return super.onKeyDown(keyCode, event);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am making a game where I have a back ground image of a
Am am making a wap-game that will have a minimap. It is basically a
I am making a game that needs a crosshair. I have been playing with
I am making a card game in ruby. I have the Game class, which
I'm planning on making a game for the iPhone and want to have the
I'm making a game with cocos2d and I have a bunch of sprites that
I am making a game on android which will have three threads. These will
I'm making a game in which you have to pick up a key first
We are making a game with a playing character that will have several different
Making game of life I need to a have a grid that is 30x20

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.