Open the DialogFragment dialog from CustomView

I am working on one of my Android projects, trying to reduce the code inside my actions. In one of them, I have a CustomView (which only extends LinearLayout), which opens the DialogFrament dialog box when clicked. Now I implement this by overriding onTouch() in action and subsequently opening DialogFrament. It looks something like this:

 @Override public boolean onTouch(View v, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_UP) { if (v.getId() == mCustomView.Id()) { mDialogFragment.show(mFragmentManager, ""); 

I would like to move the process of opening DialogFragment away from the action and to CustomView itself, but the problem is that I cannot get an instance of FragmentManager (using getSupportFragmentManager() ) in CustomView. Am I planning, or should I stick with the code that I am working on? I do this, so my code looks more comprehensible and understandable.

+4
source share
1 answer

in your custom view, you can call getContext() (which will be your activity) so that you can then send it to android.support.v4.app.FragmentActivity and call getSupportFragmentManager() from there.

So that should do it.

 android.support.v4.app.FragmentActivity fragmentActivity = (android.support.v4.app.FragmentActivity) getContext(); FragmentManager fm = fragmentActivity.getSupportFragmentManager(); 
+9
source

Source: https://habr.com/ru/post/1495341/


All Articles