简体   繁体   中英

How to access the variable that is being set in onCreate method of another class?

I have a boolean switchPref defined in the onCreate method of MainActivity class. It is a variable for setting sound, a public static variable from another class called SettingsActivity. The default value for switchPref is default to false. Is it possible that the onCreate method of GameActivity class in the same package can access this boolean after the user.

public class MainActivity extends AppCompatActivity {
    Button btnPlay;
    public static Boolean switchPref;
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
PreferenceManager.setDefaultValues(this, R.xml.preferences, false);

        // Read settings
        SharedPreferences sharedPref =
                PreferenceManager.getDefaultSharedPreferences(this);
        Boolean switchPref = sharedPref.getBoolean
                (SettingsActivity.KEY_SOUND_SWITCH, false);
        Toast.makeText(this, switchPref.toString(), Toast.LENGTH_SHORT).show();

The SettingsActivity that defines a String value KEY_SOUND_SWITCH

public class SettingsActivity extends AppCompatActivity {
    public static final String KEY_SOUND_SWITCH = "sound_switch";

In another activity GameActivity of the same package, I want to access the sharedPref of onCreate method,

Boolean switchPref = sharedPref.getBoolean
                    (SettingsActivity.KEY_SOUND_SWITCH, false);

and do something such as the following to make sure that both MainActivity and GameActivity access the same boolean value. If the boolean switchPref is false in Main, then the sounds in Main and Game are both disabled. If the boolean switchPref is true in Main, then the sounds in Main and Game activity are enabled.

public class GameActivity extends AppCompatActivity {
    Button btnTry;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game);
        btnTry=(Button) findViewById(R.id.btnTry);
        final MediaPlayer mp01 =MediaPlayer.create(this,R.raw.black);
        btnTry.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
              **if the boolean switchPref variable in MainActivity is true, then play the sound mp01** {
                mp01.start();}
            }
        });
    }
}

You need first save boolean on SharedPreferences this way:

Boolean switchPref=false;

SharedPreferences sharedPref = getSharedPreferences("Preferences", Context.MODE_PRIVATE);
sharedPref.edit().putBoolean("switch", switchPref).apply();

Then, get it back in any activity, this way:

SharedPreferences sharedPref = getSharedPreferences("Preferences", 0);
Boolean switchPref = sharedPref.getBoolean("switch", false);

And with an if-else block, do what you want:

if(!switchPref)    //false-false
        {
            ...
        }
        else       //true-false
        {
            ... play the sound mp01*
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM