Show SimpleDialogFragment message alert in a fragm

2019-09-07 06:23发布

问题:

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