tim4dev
tim4dev

Reputation: 2987

Send data from onSharedPreferenceChanged() to another UI Fragment

I play Loop Background Music in app, I use SoundPool. Also I use PreferenceFragment (and sharedPreferences of course), and UI checkbox “Background Music On/Off”.

As soon as the user remove/set the UI checkbox from Preference Fragment, background music should be to stop/start play it immediately.

Question:

How to send data from PreferenceFragment from onSharedPreferenceChanged() to another UI fragment immediately (see the diagram)? Thx.

diagram

UPD Problem solved.

Make an Object (which directly controls the sound) as Singleton. Then from onSharedPreferenceChanged called SoundPool_pause_method directly.

enter image description here

Perhaps this is not true in terms of architecture.

I (quickly) read a lot of information: Using MediaPlayer in a service, AsyncTask (I already know that it is not appropriate), Executor, ThreadPoolExecutor, FutureTask, HandlerThread.

Upvotes: 1

Views: 228

Answers (1)

Neji
Neji

Reputation: 6839

If you are in search of a quick solution then you can try firing custom broadcasts.

You can check tutorial here. Your MainUIFragment will be BroadcastListener which will listen to specific custom event broadcast which can be fired by PreferenceFragment when intended.

Declaring in manifest

 <receiver android:name="MyReceiver">
         <intent-filter>
            <action android:name="some.identifier.string">
            </action>
         </intent-filter>
  </receiver>

Custom Receiver to handle event broadcasts

public class MyReceiver extends BroadcastReceiver{
   @Override
   public void onReceive(Context context, Intent intent) {
      Toast.makeText(context, "Intent Detected.", Toast.LENGTH_LONG).show();
   }
}

Firing event from

public void broadcastIntent(View view){
      Intent intent = new Intent();
      intent.setAction("some.string.identifier"); sendBroadcast(intent);
   }

Upvotes: 1

Related Questions