'Call Fragment Dialog from Activity
I call "DialogFragment" in "fragment" which invokes "FloatingActionButton" in "Activity" and "CallBack interface". But in "Fragment" which calls "DialogFragment" returns the Null value for "GetParentFragmentManager"
In "MainActivity" there is a "ViewPager" and a "FloatingActionButton (FAB)". In "ViewPager" there is a "Fragment" and in it there is "ResyclerView". I'm trying to call "DialogFragment" from FAB. But "getParentFragmentManager" returns NULL.
"floatingActionButton in Main Activity"
` protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Repository.getInstance().defaultUser();
mUserName_TV = findViewById(R.id.TV_UserName);
mUserName_TV.setText(Repository.getInstance().getCurrentUser().getUserName());
mViewPager2 = findViewById(R.id.viewPager);
mViewPager2.setAdapter(new ViewPagerAdapter(this));
mTabLayout = findViewById(R.id.tabLayout);
mFAB_BTN = findViewById(R.id.fabButton);
mFAB_BTN.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mCallBack = new RecyclerFragment();
mCallBack.createTask(mTabLayout.getSelectedTabPosition());
}
});`
"CallBack Method in Fragment"
@Override
public void createTask(int tabSelected) {
NewTaskDialogFragment fragment = NewTaskDialogFragment.newInstance(tabSelected);
fragment.setTargetFragment(this,REQUEST_CODE_ALTER_DIALOG);
fragment.show(getFragmentManager(),TAG_DIALOG);
}
}
interface CallBack
{
public void createTask(int TabSelected);
}
"DialogFragment"
public Dialog onCreateDialog(Bundle savedInstanceState) { View view = getLayoutInflater().inflate(R.layout.fragment_alter_dialog, null);
initUi(view);
initInfo();
return new AlertDialog.Builder(getActivity())
.setView(view)
.setTitle("Create New Task")
.create();
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|