-1

long story short.

I have the mainActivity with two Fragments. So instead of the mainActivity there are the two Fragments. In the first Fragment there is a Switch.

Is it possible to update the background from the mainActivity when the switch is checked or unchecked? The Background is set in the content_main.xml, because then you have no transition when changing the fragments.

So is it at all possible?

I thought this would work:

if(switch.isChecked()) {
        SharedPreferences sharedPref = getActivity().getSharedPreferences("my_prefs", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPref.edit();
        editor.putInt("background1", R.drawable.background2);
        editor.apply();
    }

And in the MainActivity:

SharedPreferences sharedPref = getSharedPreferences("my_prefs", Context.MODE_PRIVATE);
    int bg = sharedPref.getInt("background1", R.drawable.background1);
    getWindow().setBackgroundDrawableResource(bg);

Or is it just not possible because the fragment isn't loading itself new? If so. How is it possible?

Thanks in advance guys

source:

Changing background of an activity from another activity

Hansi
  • 97
  • 3
  • 13

3 Answers3

2

Use callback interface to communicate between fragment and your activity

Please see the below code snippet :

public class YourFragment extends Fragment{

   OnCallbackReceived mCallback;

// Implement this interface in your Activity.

public interface OnCallbackReceived {
    public void Update(boolean state);
}

In your fragment :

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);

    try {
        mCallback = (OnCallbackReceived) activity;
    } catch (ClassCastException e) {

    }
}

// You can Call the event from fragment as mentioned below
// mCallback is the activity context. 
mCallback.Update(switch.isChecked());

Activity :

public class MainActivity extends Activity
    implements YourFragment.OnCallbackReceived {

// Implemented method.
public override void Update(boolean state) {
    // Update bg here
}

Original:

How to send data from fragment to activity

Anton Sarmatin
  • 505
  • 2
  • 8
2

use Otto event bus,It's easy and simple

Click this link

add this to your dependencies

implementation compile 'com.squareup:otto:1.3.8'

Activity or Fragment onCreate add this one

Bus bus = new Bus();

You can post event like this from your activity or fragment

bus.post(new AnswerAvailableEvent(42));

if you want to get events even when your fragment is in the foreground or background, Subscribe to the events like below

@Subscribe public void answerAvailable(AnswerAvailableEvent event) {
// TODO: React to the event somehow!
}

Don't forget to register and unregister in onStart and onStop

@Override
protected void onStart(){
 super.onStart();
 bus.register(this);
}

@Override
protected void onStop(){
 super.onStop();
 bus.unregister(this);
}
Harish Reddy
  • 922
  • 10
  • 20
  • 1
    Although using an EventBus could solve his problem, your answer is link only, try to improve it. For more information, see ["How do I write a good answer?"](https://stackoverflow.com/help/how-to-answer) in the Help Center. – Mauker Dec 18 '18 at 15:11
1

Yes, it's possible to achieve that. I think the easiest way to do so is through a listener/callback.

First of all, create your listener class:

public interface MyCallback {
    void onSwitchStateChanged(boolean isChecked);
}

After that, make your Activity implement that interface, and implement your background changing logic inside the onSwitchStateChanged method.

Now, inside your Fragment's onAttach() method, do the following (this example is in Kotlin):

override fun onAttach(context: Context?) {
    super.onAttach(context)
    if (context is MyCallback) {
        myListener = context as MyCallback
    }
    else {
        throw RuntimeException("Must implement MyCallback!")
    }
}

Where myListener is a variable inside your fragment.

Now add a setOnCheckedChangeListener on your switch, like it's described on this answer, and use the callback inside it. For example (in Java):

mySwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        myListener.onSwitchStateChanged(isChecked);
    }
});
Mauker
  • 11,237
  • 7
  • 58
  • 76
  • Great thank you! To switch it back I've added in the `onCheckedChanged` method `if(isChecked == true) { background.setBackgroundResource(R.drawable.background1); } else { background.setBackgroundResource(R.drawable.background2); }`. So it's perfectly working. Thanks again! – Hansi Dec 18 '18 at 18:08