0

My app has a bottomNavigationBar with three fragments. My MainActivity contains a CollapsingToolbar with a RadioGroup inside. Now I get the value of the selected RadioButton in the MainActivity, but I need the value to work with it inside my first fragment. How do I do that? Does every fragment contains its own CollapsingToolbar or is the data passed between the activities?

Mike Kng
  • 255
  • 2
  • 11
  • Possible duplicate of [Send data from activity to fragment in android](https://stackoverflow.com/questions/12739909/send-data-from-activity-to-fragment-in-android) – Sakchham Jul 03 '18 at 18:56

4 Answers4

4
  1. You can use the fragment's setArguments(Bundle) method where the Bundle has key-value pairs that you've set. For example, your fragment object is yourFragment then you have

    Bundle bundle = new Bundle();
    bundle.putString("paramKey", "paramVal");
    
    yourFragment.setArguments(bundle);
    
  2. In the fragment's onCreateView(LayoutInflater, ViewGroup, Bundle) you can access the information with the getArguments() method.

    String value = getArguments().getString("paramKey");  // value = "paramVal"
    // inflate, return
    

Have a look at the documentation for more information on setting bundle values.

geco17
  • 5,152
  • 3
  • 21
  • 38
  • Is it possible to pass arrays or arrayLists like that as well, or only single values? – Mike Kng Jul 03 '18 at 18:54
  • 1
    have a look at the [documentation](https://developer.android.com/reference/android/os/Bundle), in particular **putStringArrayList**. – geco17 Jul 03 '18 at 19:00
  • Ah that sounds good, thanks! I see when the fragment is created it fetches the value, but what if the value changes while i‘m in the fragment? It doesn’t restart then, does it? – Mike Kng Jul 04 '18 at 13:00
  • @MikeKng check my answer for the case when your value changes while you're in fragment – Mayank Bhatnagar Jul 05 '18 at 01:51
1

You can use put the values you want to pass in SharedPreferences

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    SharedPreferences.Editor edit = prefs.edit();
    edit.putString("some_key",someValue); //someValue is a var that containns the value that you want to pass
    edit.commit();

Then in your fragment, access the value:

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
String value = prefs.getString("some_key","default_value");

Another rather less efficient way

Create a Utility class that will hold all your static variables. You will be able to set and get the values of these variables in all instances of that class

1

This can also be achieved using Java interfaces like this

  1. Have an Interface defined in your Activity class. Capture the interface instance while committing your fragment which will later be used to send data to fragment

.

  class ExampleActivity extends Activity {

        //Data listener to be implemented by the fragment class
        public interface OnDataListerner{
            public void sendData(ArrayList<String> data);
        }

        //DataListener instance to be captured while committing fragment
        OnDataListener fragment;

        //commit your fragment and type cast it to OnDataListener
        private void commit Fragment(){
           fragment = new ExampleFragment();
           FragmentTransaction transaction = 
           getSupportFragmentManager().beginTransaction();
           transaction.replace(R.id.fragment_container, exampleFragment);
           transaction.addToBackStack(null);
           transaction.commit();
        }

        //used to send data through interface methods which will be defined in fragment
        public void sendDataToFragment(){
           fragment.sendData(Your data to be send);
        }

 }
  1. Have this interface implemented in your Fragment class and once Acitivity calls any method on this interface it will be called in this Fragment

    public class ExampleFragment extends Fragment implements ExampleActivity.OnDataListerner {

    //interface callback which is called when Activity call its method. 
    public void sendData(ArrayList<String> data){
          //Here is your data which can be consumed 
    }
    

    }

Hope this helps.

Mayank Bhatnagar
  • 2,120
  • 1
  • 12
  • 20
0

You can create a method in the fragments visible to the activity class. Then using this method, you can pass the values. And in implementation of this function, you can do required changes such as UI updates in the fragment.

For example -

MainActivity.java

// in member declarations
private MyFragment frag;
private CentralFragment cFrag;

// initialize the fragment
frag = new MyFragment(args);
cFrag = new CentralFragment(otherArgs);

// onRadioButtonClicked -> assuming selected value = v
frag.onChoice(v);
cFrag.onChoice(v);

MyFragment.java

public void onChoice(Type arg) {
    // use the value 
    someTextView.setText(arg);
}

CentralFragment.java

public void onChoice(Type arg) {
    // use the value 
    otherTextView.set(arg);
}
rushi
  • 142
  • 1
  • 12