Bohua Jia
Bohua Jia

Reputation: 77

Get value from one activity to another one

How can I use savePreferences and loadPreferences methods In first activity

private static final String GLOBAL_PREFERENCES = "music_status";

public static void savePreferences(@NonNull Context context, String key, int value) {
    SharedPreferences sharedPreferences = context.getSharedPreferences(GLOBAL_PREFERENCES, Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putInt(key, value);
    editor.apply();
}...
protected void onResume() {
    super.onResume();

    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) {
            switch (checkedId){
                case R.id.radioButtonMusicOff:
                    mediaPlayer.pause();
                    savePreferences(MainMenuActivity.this,"music_status",0);
                case R.id.radioButtonMusicOn:
                    mediaPlayer.start();
                    savePreferences(MainMenuActivity.this,"music_status",1);
            }
        }
    });

}

In Second activity

private static final String GLOBAL_PREFERENCES = "music_status";
public static int loadPreferences(@NonNull Context context, String key, int defaultValue) {
    SharedPreferences sharedPreferences = context.getSharedPreferences(GLOBAL_PREFERENCES, Context.MODE_PRIVATE);
    return sharedPreferences.getInt(key, defaultValue);
}

However I do not know how to get the value from first activity

Upvotes: 3

Views: 87

Answers (3)

Knossos
Knossos

Reputation: 16048

The short answer is you cannot do that. Unless your status variable is static. Which is extremely bad for this case.

You have three choices.

SharedPreferences

In contrast to my previous revision. This might be the best option for your case.

If you only want to save the state of something, you might find it better to simply not use a class to hold the status of your music. You could do as @cricket_007 suggests and implement SharedPreferences.

You could use these example functions:

private static final String GLOBAL_PREFERENCES = "a.nice.identifier.for.your.preferences.goes.here";

public static void savePreferences(@NonNull Context context, String key, int value) {
    SharedPreferences sharedPreferences = context.getSharedPreferences(GLOBAL_PREFERENCES, Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putInt(key, value);
    editor.apply();
}

public static int loadPreferences(@NonNull Context context, String key, int defaultValue) {
    SharedPreferences sharedPreferences = context.getSharedPreferences(GLOBAL_PREFERENCES, Context.MODE_PRIVATE);
    return sharedPreferences.getInt(key, defaultValue);
}

You can then use those functions in your code to save the status of your music. Instead of status.setStatus(0); and status.setStatus(1); your can use Utils.savePreferences(context, "music_status", 1); and instead of status.getStatus() you could use Utils.loadPreferences(context, "music_status", 0);

Parcelable

One option for you is to implement Parcelable in your musicStatus class. You can then send the object through an Intent to your second activity. You may find an example class implementing Parcelable below.

Once you have that, you can pass it through Intents like so:

musicStatus status = new musicStatus();
status.setStatus(8);
Intent intent = new Intent(this, HighScoreActivity.class);
intent.putExtra("status", status);
startActivity(intent);

Class implementation:

public class musicStatus implements Parcelable {
    private int status;

    public int getStatus() {
        return status;
    }

    public void setStatus(int status){
        this.status = status;
    }

    private musicStatus(Parcel in) {
        status = in.readInt();
    }

    public void writeToParcel(Parcel out, int flags) {
        out.writeInt(status);
    }

    public static final Parcelable.Creator<musicStatus> CREATOR = new Parcelable.Creator<musicStatus>() {
        public musicStatus createFromParcel(Parcel in) {
            return new musicStatus(in);
        }

        public musicStatus[] newArray(int size) {
            return new musicStatus[size];
        }
    };

    public int describeContents() {
        return 0;
    }
}

Singleton

In this case, this would really be an anti-pattern. However, it is still a possibility.

public class musicStatus {
    private static musicStatus mInstance = null;
    private int status;

    @NonNull
    public static musicStatus getInstance() {
        if (mInstance == null) {
            synchronized(mInstance) {
                if (mInstance == null) {
                    mInstance = new musicStatus();
                }
            }
        }
    }

    public int getStatus(){
        return status;
    }

    public  void setStatus(int status){
        this.status = status;
    }
}

Upvotes: 4

PLe
PLe

Reputation: 137

For the easiest way, your musicStatus class need implement Serializable.

Bundle bundle = new Bundle();
bundle.putSerializable("musicStatus", status);
intent.putExtras(bundle);

in other activity:

Intent intent = this.getIntent();
Bundle bundle = intent.getExtras();

musicStatus status=
               (musicStatus)bundle.getSerializable("musicStatus");

Upvotes: 2

Prashant Jajal
Prashant Jajal

Reputation: 3627

extend Serviceable in you POJO class.

After while starting Activity 2.

  intetn.putExtra("obj",pojoObj);

and get it in you second activity using.

PojoCls obj = (PojoCls)getIntent().getSerializableExtra("obj");

Thats it.

Upvotes: 0

Related Questions