I have a problem with Fragments.
Problem what i am facing is, i have a simple DialogFragment
public class Fragment3 extends DialogFragment {
private Fragment activity;
ViewGroup root;
static Fragment3 newInstance() {
Fragment3 f = new Fragment3();
return f;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
root = (ViewGroup) inflater.inflate(R.layout.fragment3_layout, container, false);
return root;
}
}
And i want to create this Fragment from another Fragment
public class ListViewImageAdapter extends BaseAdapter {
private Fragment activity;
....
public ListViewImageAdapter(Fragment activity, ArrayList<Object> listImages) {
this.activity = activity;
...}
....
FragmentTransaction ft = activity.getFragmentManager().beginTransaction();
Fragment3 newFragment = Fragment3.newInstance();
newFragment.show( ft, "dialog");
....
But I get a message:
The method show(FragmentManager, String) in the type DialogFragment is not applicable for the arguments (FragmentTransaction, String)
that should mean its in the foreground displaying if im not mistaken. If you call DialogFragment's creation several times in one moment, dialogFragment = getSupportFragmentManager(). findFragmentByTag("dialog"); will return null, and all dialogs will be shown.
tl;dr: The correct way to close a DialogFragment is to use dismiss() directly on the DialogFragment. Control of the dialog (deciding when to show, hide, dismiss it) should be done through the API here, not with direct calls on the dialog. Dismiss the fragment and its dialog.
The FragmentManager manages the fragment back stack. At runtime, the FragmentManager can perform back stack operations like adding or removing fragments in response to user interactions. Each set of changes are committed together as a single unit called a FragmentTransaction .
import android.support.v4.app.FragmentActivity;
instead of:
import android.app.DialogFragment;
(or the reverse)
Try to call newFragment.show( activity.getFragmentManager(), "dialog");
instead of newFragment.show( ft, "dialog");
. DialogFragment.show() method takes FragmentManager
as first argument, not FragmentTransaction
. Please take a look on tutorial
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With