0

From my first activity:

musicGroup = (RadioGroup) findViewById(R.id.radioGroupForMusic);
turnOn = (RadioButton) findViewById(R.id.radioButtonMusicOn);
turnOff = (RadioButton) findViewById(R.id.radioButtonMusicOff);
musicGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
   @Override
   public void onCheckedChanged(RadioGroup group, int checkedId) {
     if(turnOff.isChecked()) {
       int musicOff = 0;
       mediaPlayer.pause();
       Intent intent = new Intent(MainMenuActivity.this, HighScoreActivity.class);
       intent.putExtra("music off",musicOff);
       startActivity(intent);
     }
     else if(turnOn.isChecked()) {
       int musicOn = 1;
       mediaPlayer.start();
       Intent intent = new Intent(MainMenuActivity.this, HighScoreActivity.class);
       intent.putExtra("music on",musicOn);
       startActivity(intent);
     }
   }
});

and this is my second activity:

Intent intent = getIntent();
int musicOn = intent.getIntExtra("music on",0);
if(musicOn==1) {
   mediaPlayer = MediaPlayer.create(HighScoreActivity.this,R.raw.song);
   mediaPlayer.setLooping(true);
   mediaPlayer.start();
}

At the moment, when I choose whatever radio button, it will navigate to my second activity, which is not what I want, I just want to control the background music from the first activity and make it effects on other activities. I think I should not use Intent as it will always navigate to second activity

Thanks for any suggest.

Bohua Jia
  • 77
  • 8

7 Answers7

1
Intent intent = new Intent(getBaseContext(), SignoutActivity.class);
intent.putExtra("EXTRA_SESSION_ID", value);
startActivityForREsult(intent)
Daniel Nugent
  • 43,104
  • 15
  • 109
  • 137
0

The best way to pass value to another activity is using Bundle.

Just create a Bundle object and add your passing parameters to it.

Bundle bundle = new Bundle();

add value to that bundle like
bundle.putString("key","value");
bundle.putInt("keyInt",value);

Now Create an Intent Object and pass bundle object to putExtra Method of intent object.

Intent intent = new Intent(MainMenuActivity.this, HighScoreActivity.class);
intent.putExtras(bundle);

startActivity(intent);

This way you will be able to send values to other Activity.

Now to get passed values to another activity.

    Intent intent = getIntent();
    Bundle bundle =  intent.getExtras();
    String strVal = bundle.getString("key");
    int bytes = bundle.getInt("keyInt");
0

Please try this way. Changes in first activty.

musicGroup=(RadioGroup)findViewById(R.id.radioGroupForMusic);
musicGroup.setOnCheckedChangeListener(new  RadioGroup.OnCheckedChangeListener(){
    @Override
    public void onCheckedChanged(RadioGroup group,int checkedId){
        boolean  isMediaPlayerON=false;
        switch(checkedId){
            case R.id.radioButtonMusicOn:
                mediaPlayer.start();
                isMediaPlayerON=true;
            break;
            case R.id.radioButtonMusicOff:
                mediaPlayer.pause();
                isMediaPlayerON=false;
            break;
        }
        Intent intent=new Intent(MainMenuActivity.this,HighScoreActivity.class);
        intent.putExtra("music",isMediaPlayerON);
        startActivity(intent);
    }
});

and in second activity

Intent intent = getIntent();
int isMediaPlayerON = intent.getBooleanExtra("music",false);
if(isMediaPlayerON){
   mediaPlayer = MediaPlayer.create(HighScoreActivity.this,R.raw.song);
   mediaPlayer.setLooping(true);
   mediaPlayer.start();
}
Bhavesh Desai
  • 753
  • 4
  • 20
0

Do this.

int musicOnOff = 1;

musicGroup = (RadioGroup) findViewById(R.id.radioGroupForMusic);
turnOn = (RadioButton) findViewById(R.id.radioButtonMusicOn);
turnOff = (RadioButton) findViewById(R.id.radioButtonMusicOff);
musicGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        if(turnOff.isChecked()){
            musicOnOff = 1;
            mediaPlayer.pause();
            Intent intent = new Intent(MainMenuActivity.this, HighScoreActivity.class);
            intent.putExtra("musiconoff", musicOnOff);
            startActivity(intent);
        }
        else if(turnOn.isChecked()){
            musicOnOff = 2;
            mediaPlayer.start();
            Intent intent = new Intent(MainMenuActivity.this, HighScoreActivity.class);
            intent.putExtra("musiconoff",musicOnOff);
            startActivity(intent);
        }
    }
});

In the second Activity catch the value by

int musicOn = getIntent().getExtras().getInt("musiconoff");
if(musicOn == 1){
    mediaPlayer = MediaPlayer.create(HighScoreActivity.this,R.raw.song);
    mediaPlayer.setLooping(true);
    mediaPlayer.start();
}

Instead of using two keys for the String passed through the Intent, just use one key, which is "musiconoff".

And remember to use one variable, which is "musicOnOff", to switch the value when Radio Buttons are checked. Thus even if you did not check a Radio Button the variable has a default value which should be the value according to the checked Radio Button.

EDIT: You can switch between **Radio Buttons** this way, in order to prevent navigation to the second Activity and this will only navigate to the second when they are checked.

RadioGroup.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch(checkedId){
            case R.id.radioButtonMusicOn://For Turn On
                //Navigate from here
                break;

            case R.id.radioButtonMusicOff://For Turn Off
                //Navigate from here
                break;
        }
    }
});
vidulaJ
  • 1,232
  • 1
  • 16
  • 31
  • I try this code, but when radio button be pressed, the program still navigate to my second activity, not stay the first activity and affect second one – Bohua Jia Oct 18 '16 at 05:15
  • It still navigates to the second Activity as you have checked conditions using the Radio Button handler, which are turnOn and turnOff. It is recommended to use the other way of switching among the checkIDs. See the edit. – vidulaJ Oct 18 '16 at 05:26
  • I think I **should not** use `Intent` as it will navigate which I dont want – Bohua Jia Oct 18 '16 at 06:40
0

First approach : SharedPreferences

You should use SharedPreferences for your purpose

 Boolean MusicIsOff = true; // whatever you like
 SharedPreferences sf=getSharedPreferences("APP_NAME",0);
 sf.edit().putBoolean("music",MusicIsOff).commit();

And later in your activities you can check it by this:

SharedPreferences sf=getSharedPreferences("APP_NAME",0);
Boolean musicIndicatior=sf.getBoolean("music",false); // false means if i never set this parameter, what is the default value

you can access it anywhere you like.

Second approach : Broadcast Receivers

There is also another approach which will solve your problem. using Broadcast receivers. i advise you to use the EventBus library as its so easy to use and will handle everything for you. You can use it by just adding a few lines to your project.

first add this to your gradle file:

compile 'org.greenrobot:eventbus:3.0.0'

then in all your activities in which you want to receive the broadcast when user change the radio button, add these lines.

@Override
public void onStart() {
   super.onStart();
   EventBus.getDefault().register(this);
}

@Override
public void onStop() {
  super.onStop();
  EventBus.getDefault().unregister(this);
}

@Subscribe(threadMode = ThreadMode.MAIN)  
public void onMessageEvent(Boolean event) {
   /* Do something */
};

And at the end, when user change the radio button do this:

Boolean music=true;
EventBus.getDefault().post(music);
Omid Heshmatinia
  • 5,089
  • 2
  • 35
  • 50
  • I try first one, maybe misunderstand, I do this way but it doesnt work. – Bohua Jia Oct 18 '16 at 05:57
  • `public void onCheckedChanged(RadioGroup group,int checkedId){ if(turnOff.isChecked()){ mediaPlayer.pause(); }else if(turnOn.isChecked()){ mediaPlayer.start(); SharedPreferences sf=getSharedPreferences("splitBall",0); sf.edit().putBoolean("music",true).commit(); } }` – Bohua Jia Oct 18 '16 at 05:58
  • in second act do this – Bohua Jia Oct 18 '16 at 05:58
  • `SharedPreferences sf=getSharedPreferences("splitBall",0); Boolean musicIndicatior=sf.getBoolean("music",false); if(musicIndicatior==true){ mediaPlayer = MediaPlayer.create(HighScoreActivity.this,R.raw.song); mediaPlayer.setLooping(true); mediaPlayer.start(); }` – Bohua Jia Oct 18 '16 at 05:59
  • when using shared Preference instead of true , you should put your desired VARIABLE ! see my edit . @BohuaJia – Omid Heshmatinia Oct 18 '16 at 09:41
0

Though other comments say it's better using Broadcast Receiver (I think they mean Local Broadcast), I should advise you to use EventBus it is because unlike Android’s BroadcastReceiver/Intent system, EventBus uses standard Java classes as events and offers a more convenient API.

you only need to send the message to the receiving Activity with something like:

EventBus.getDefault().post(new MusicStatusEvent(status));

in your onCheckedChanged(RadioGroup group, int checkedId), so it will become:

public void onCheckedChanged(RadioGroup group, int checkedId) {
  // Here you send the message if music is on or off.
  EventBus.getDefault().post(new MusicStatusEvent(turnOff.isChecked());
}

and the receiving activity must be implementing:

@Subscribe(threadMode = ThreadMode.MAIN)  
public void onMessageEvent(MusicStatusEvent event) {
  /* Do something after you receive the message */
  Log.d("status", "music is" + event.getStatus()? "on" : "off");
};

The MusicStatusEvent just a simple pojo class:

public static class MusicStatusEvent {
   private boolean status;

   public MusicStatusEvent(boolean status) {
     this.status = status;
   }
   public void getStatus() {
     return status;
   }
}

for a description of EventBus, you can read from https://stackoverflow.com/a/38122248/4758255

If you really want to use Broadcast (though I do not recommend it), you can look at https://stackoverflow.com/a/25246377/4758255

Community
  • 1
  • 1
ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
0

Try this

In Your First Activity:-

musicGroup = (RadioGroup) findViewById(R.id.radioGroupForMusic);
turnOn = (RadioButton) findViewById(R.id.radioButtonMusicOn);
turnOff = (RadioButton) findViewById(R.id.radioButtonMusicOff);
musicGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        if(turnOff.isChecked()){
            mediaPlayer.pause();
            Intent intent = new Intent(MainMenuActivity.this, HighScoreActivity.class);
            intent.putExtra("musicOff",true);
            startActivity(intent);
        }
        else if(turnOn.isChecked()){
            mediaPlayer.start();
            Intent intent = new Intent(MainMenuActivity.this, HighScoreActivity.class);
            intent.putExtra("musicOff",false);
            startActivity(intent);
        }
    }
});

And in second activity:-

Boolean musicOff = getIntent().getBooleanExtra("musicOff", true);
if(musicOff){
mediaPlayer = MediaPlayer.create(HighScoreActivity.this,R.raw.song);
mediaPlayer.setLooping(true);
mediaPlayer.start();
}