Show SimpleDialogFragment message alert in a fragment

冷暖自知 提交于 2019-12-11 14:54:05

问题


I have a Main activity that extends from FragmentActivity, like this:

public class Main extends FragmentActivity{
public static FragmentManager fragmentManager;
public static CustomFragmentAdapter mAdapter;
...
//I can access to canAccessFromMain() in SecondTab like this:
((SecondTab)Main.mAdapter.getRegisteredFragment(1)).canAccessFromMain();
}

With two fragments, like this:

public class SecondTab extends Fragment implements ISimpleDialogListener{
...
//Access to objects in Main like this:
googleMap = ((SupportMapFragment)Main.fragmentManager.findFragmentById(R.id.map)).getMap();
...

@Override
public void onPositiveButtonCLicked(int requestedCode){
if(requestedCode == 12}{
//Do something //Problem is here, it's never called!
}
...

public void canAccessFromMain(){
mImageView.setVisibility(View.invisible);
}

private void DoSomething(){
//I test all of these, but not working:
//getActivity(),getActivity().getSupportFragmentManager()
//getActivity(),((SecondTab )Main.mAdapter.getRegisteredFragment(1))).getFragmentManager()
//Main.fragmentManager
SimpleDialogFragment.createBuilder(getActivity(),getActivity().getSupportFragmentManager()
.setTitle("ttt")
.setMessage("mmm")
.setPositiveButtonText("OK").setRequestCode(12)
.setNegativeButtonText("No")
.show();

...
}

And this is CustomFragmentAdapter class:

//liek this: http://stackoverflow.com/questions/8785221/retrieve-a-fragment-from-a-viewpager
class CustomFragmentAdapter extends FragmentStatePagerAdapter{
...
public Fragment getRegisteredFragment(int position){
return registeredFragments.get(posotion);
}

I'm using this library to show a alert message: https://github.com/inmite/android-styled-dialogs

Why when i press positive or negative button that show in secondtab fragment, nothings happed?


回答1:


You forgot to call method setTargetFragment() in your builder.




回答2:


You should implement an OnClickListener



来源:https://stackoverflow.com/questions/25099494/show-simpledialogfragment-message-alert-in-a-fragment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!