Skip to content Skip to sidebar Skip to footer

Access Main Activity View From Fragment

I have an activity that has container that contain fragments and this fragment has other fragments. Now I want this second or child fragment to access views in main activity, but i

Solution 1:

It is not good practise to find and control a view in this way. Views can easily become detached from activities and cause unexpected exceptions.

You should rather look at using callbacks to communicate between fragments and activities if required. That way, it also keeps your code in the correct places - so the activity is the only one touching its own views and the fragment also only touches its own views. It merely tells the activity (via callbacks) that something has happened that the activity might want to know about. It also ensures that the fragments are completely self contained and can be easily reused.

You can read about how to implement callbacks here: http://developer.android.com/training/basics/fragments/communicating.html

Solution 2:

Use EventBus to communicate between the activity and the fragment. riggarro suggestion is the correct way. But you can also able to update the base activity views using the EventBus.

For example we need to update a TextView text in a activity from the fragment, follow the steps.

  1. First you need to add the following library as dependency to your project in build.gradle of your app.

compile 'de.greenrobot:eventbus:2.4.0'

  1. First you need to create a Event Object class to communicate between the fragment and activity like below.
publicclassUpdateTextEvent {
    privateString sampleTextValue;
    publicUpdateTextEvent(String textValue) {
        this.sampleTextValue = textValue;
    }
    publicStringgetTextValue() {
        return sampleTextValue;
    }
}
  1. You need to post a event to the event bus in the fragment to update the TextView in the activity.
publicclassTestingFragmentextendsFragment{
    privateEventBusbus= EventBus.getDefault()
    publicTextingFragment(){}
    publicvoidonCreate(Bundle onSavedInstanceState){
        super.onCreate(onSavedInstanceState);
    }
    public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState){
        Viewv= inflater.inflate(R.layout.sample_activity, parent, false);
        ...
        Buttonb1= (Button) v.findViewById(R.id.button1);
        b1.setOnClickListener(newView.OnClickListener() {
            @OverridepublicvoidonClick(View v) {
                //trigger a update to the activity
                bus.post(newUpdateTextEvent("testing"));
            }
        });
    }
}
  1. After that you need to register the bus with the callback of the event in the activity like below.
publicclassMainActivityextendsActivity{
    privateEventBusbus= EventBus.getDefault();
    private TextView textView;
    @OverridepublicvoidonCreate(Bundle onSavedInstanceState){
        super.onCreate(onSavedInstanceState);
        ....
        // The textview going to be updated on posting the event
        textView = (TextView) findViewById(R.id.text1);
        bus.register(this);
    }
    publicvoidonEvent(UpdateTextEvent event){
        textView.setText(event.getTextValue());
    }
}

In this above example the onEvent method will be called when you post a event from the fragment..

Hope it will help you.

Post a Comment for "Access Main Activity View From Fragment"