Reputation: 678
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;
}
}
}
Upvotes: 18
Views: 16115
Reputation: 1843
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.
Upvotes: 12
Reputation: 14403
I'm trying to use findFragmentByTag
to find the fragment, but it's not easy to add tag in my project and the fragment is one custom fragment class. so i iterate the fragments to find it. The code snippet is as below:
MyFragment myFrag = null;
List<Fragment> fragments = getContext().getSupportFragmentManager().getFragments();
for (Fragment frag : fragments) {
if (frag instanceof MyFragment) {
myFrag = (MyFragment) frag;
break;
}
}
Upvotes: -1
Reputation: 41
Use the constructor of the view to pass the fragment itself no its activity. It is a deep copy so you will be able to access your fragment from the view.
public class YourView extends View
{
Fragment yourfragmentinside;
public YourView(Fragment inputyourfragment)
{
this.yourfragmentinside = inputyourfragment
}
}
Don't worry it is the same fragment -if you don't use new...()-
Upvotes: 0
Reputation: 3210
i find the solution and try it with me
in the constructor of the Custom view get reference of the context and cast it to Activity Class
private Activity activity ;
public EasCalWeekViewHeader1(Context context){
activity = (Activity) context ;
}
And in the method you want to call Fragment method
yourFragment a= activity.getFragmentManager().findFragmentById(R.id.fragment1);
a.YourMethod();
Upvotes: 2
Reputation: 6554
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.
Upvotes: 9