Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing fragment reference inside a custom view

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;
        }
    
}

}

like image 539
Manish Avatar asked Nov 30 '13 12:11

Manish


2 Answers

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.

like image 131
Michael Geier Avatar answered Nov 02 '22 18:11

Michael Geier


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.

like image 9
VJ Vélan Solutions Avatar answered Nov 02 '22 17:11

VJ Vélan Solutions