Access to fragment link inside user view

I have a scenario in which a fragment has its own view (by extending the View class) inside its layout. The user view must access some fragment methods. But I can not get the link to the fragment inside the user view. Also, I cannot access the fragment from the user view (which is a child of the fragment)

According to the android fragments: we get the context inside the view constructors. This context is an example of activity. But there is no way to get a link to the fragment on which the user view is placed.

Please let me know how to access the fragment inside the user view.

EDIT:

Adding code to better understand 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 activity insatnce inside this customView and can call any method of activity instance. //But we cannot do this for fragments. There is not way to access parent fragment at this point. I cannot access selectedOptionIndex and use it to set in this view. //if view can access the parent activty, then it should also have an access to parent fragment.
}

 /** Default Constructor */ public EasCalWeekViewHeader1(Context context, AttributeSet attrs) { super(context,attrs); this.context = (MyActivity) context; } } 

}

+8
android android-fragments android-custom-view
source share
3 answers

OK In your own words:

According to the Android fragments: we get the context inside the View constructors. This context is an example of activity. But there is no way to get a link to the fragment that hosts the user view.

So context is an instance of an action, right? Now, from this activity context, I believe you can get a link to your fragment if you provided a fragment with a string tag name or identifier using findFragmentByTag(String tag) and findFragmentById(int id) respectively.

 context.getFragmentManager().findFragmentByTag("tag_of_frag_you_are_trying_to_access"); 

NTN.

+7
source share

I will find a solution and try it with me

in the custom view constructor, get a link to the context and pass it to the Activity Class

 private Activity activity ; public EasCalWeekViewHeader1(Context context){ activity = (Activity) context ; } 

And in the method you want to call the fragment method

 yourFragment a= activity.getFragmentManager().findFragmentById(R.id.fragment1); a.YourMethod(); 
+2
source share

Use the view constructor to pass the fragment itself without its activity. This is a deep copy, so you can access the fragment from the view.

 public class YourView extends View { Fragment yourfragmentinside; public YourView(Fragment inputyourfragment) { this.yourfragmentinside = inputyourfragment } 

}

Don’t worry, this is the same snippet if you are not using a new one ... () -

0
source share

All Articles