0

I have 3 Activities, let the Activity A is the main Activity from which B and C Activities are called. B is Activity with settings, and C is Activity with the main actions. I need to realize ToggleButton in B which is responsible for whether the device will vibrate after pressing the buttons in C. Thus, it is necessary to connect B and C. If using Intent, it is necessary to call the StartActivity (Intent)/StartActivityForResult (Intent) method. From this it follows that when pressing ToggleButton in B, the C will be called by B. And it is unnecessary to me. I need that when pressing ToggleButton "something" was remembered "somewhere", and then when the C is called it will be cause to device vibrating. How to solve this problem?

Denis Lolik
  • 299
  • 1
  • 4
  • 11
  • If you need the value to persist then use sharedPreference. Alternatively, you can use static variables. Check this http://www.infoq.com/presentations/Android-Design – Zohra Khan Feb 04 '14 at 18:51
  • Check this link http://stackoverflow.com/questions/12189476/public-static-variables-and-android-activity-life-cycle-management – Zohra Khan Feb 04 '14 at 18:52

2 Answers2

1

SharedPreferences is probably the best way to handle this. In Activity B create a SharedPreferences for vibrate, set to the boolean value "true" when the toggle is pressed, and then in Activity C check the SharedPreferences for said value and act accordingly.

6thSigma
  • 248
  • 1
  • 7
1

You can use SharedPreferences to store some states, eg:

SharedPreferences prefs = getSharedPreferences("myprefs", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();

editor.putString("key_string", "jack");
editor.putInt("key_int", 30);
editor.putBoolean("vibrator", false);

editor.commit();

Then you can read it when you need it:

SharedPreferences prefs = getSharedPreferences("myprefs", Context.MODE_PRIVATE);

String name = prefs.getString("key_string", "defaultName");
int age = prefs.getInt("key_int", 25);
boolean vib = prefs.getBoolean("vibrator", true);

Basically, SharedPreferences store key-value pairs. Read more about them here and here.

Melquiades
  • 8,496
  • 1
  • 31
  • 46