Shubham Dubey
Shubham Dubey

Reputation: 82

How to access parent view of activity in fragment through dataBinding

I am using data binding and i want to access activity view in my fragment using data binding because i want to display some message on the main container of activity.

Upvotes: 2

Views: 561

Answers (1)

Abdul-Aziz-Niazi
Abdul-Aziz-Niazi

Reputation: 3172

You can update it using Interfaces For the first create an interface in your fragment ILayoutUpdater with a method update() and send your params to it.

public interface ILayoutUpdater{
 void update(String update);
}

Implement the interface in your fragment's activity you'll need to override your the update method in activity

@Override
public void update(String update){
binding.myText.setText(update);
}

Now back in your fragment create a class variable of this interface and initialize it in onAttach(Context ctx) and then use layoutUpdater.update("data") where ever you want from your fragment.

private ILayoutUpdater layoutUpdater;

@override
public void onAttach(Context context){
layoutUpdater = context;
}

Upvotes: 1

Related Questions