I have a scenario where a Fragment has a customview(by extending View class) inside its layout. The custom view has to access some methods of the fragment. But I am not able to get fragment references inside a custom view. Also, I am not able to get anyway to access the fragment from the custom view(which is a child of the fragment)
As per android-fragments: we get context inside the constructors of the views. This context is an instance of the activity. But there is no way to get the reference of the fragment which is hosting the customview.
Please let me know how to access fragments inside a customview.
EDIT:
Adding the code for a better understanding of my problem:
MyFragment .java
public class MyFragment extends Fragment {
int selectedOptionIndex;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//get value of selectedOptionIndex from bundle and save it to this.selectedOptionIndex;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
this.activity = activity;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.myFragmentview, container, false);
return view;
`//i can access views from fragment, but not vice versa.`
}
}
myFragmentview.xml
<com.test.CustomView
android:id="@+id/myCustomView" android:layout_width="fill_parent"
android:layout_height="40dip" layout_weight="1"
/>
public class CustomView extends View {
private MyActivity context;
/** Default Constructor */
public EasCalWeekViewHeader1(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.context = (MyActivity) context;
// Now using context, we can access the activity instance inside this customView and can call any method of activity instance. //But we cannot do this for fragments. There is no way to access the parent fragment at this point. I cannot access selectedOptionIndex and use it to set in this view. //if the view can access the parent activity, then it should also have access to the parent fragment.
}
/** Default Constructor */
public EasCalWeekViewHeader1(Context context, AttributeSet attrs) {
super(context,attrs);
this.context = (MyActivity) context;
}
}
}
Call FragmentManager.findFragment(this)
, where this
is your custom view.
Or if you use Kotlin's Fragment library androidx.fragment:fragment-ktx
, you can just use the extension function View.findFragment()
inside your view.
It is defined as:
fun <F : Fragment> View.findFragment(): F = FragmentManager.findFragment(this)
From the documentation:
This method will locate the Fragment associated with this view. This is automatically populated for the View returned by Fragment.onCreateView and its children.
OK. In your own words:
As per android fragments: we get context inside the constructors of the views. This context is instance of the activity. But there is no way to get reference of the fragment which is hosting the customview.
So, the context is the instance of the activity, right? Now from this activity context, i believe you can get the reference to your fragment if you had provided your fragment with a string tag name or an id using findFragmentByTag(String tag)
and findFragmentById(int id)
respectively.
context.getFragmentManager().findFragmentByTag("tag_of_frag_you_are_trying_to_access");
HTH.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With