How can I show a DialogFragment using compatibilit

2019-06-18 00:56发布

问题:

I've tried to use DialogFragment on 3.0- devices, which not supports Fragment or DialogFragment by SDK level.

So, I decided to use Android compatibility library, which supports Fragment.
Then I created a DialogFragment class that extends android.support.v4.app.DialogFragment.

But.....When I tried to show my DialogFragment using DialogFragment.show(), I notified that show(FragmentManager, String) accepts first argument as android.app.FragmentManager, not android.support.v4.app.FragmentManager.

I think android.app.FragmentManager cannot be used in Android 3.0- devices, because it is not included in SDK.

Is there any way to show DialogFragment with Compatibility library? Am I have to use another way to show My DialogFragment with compatibility library?

Any help will be much appreciated. :)

回答1:

The compatibility package is for those developing on Android versions prior to 3.0.

Both the FragmentManager and the DialogFragment classes exist in API level 11 (3.0)

In any Fragment or Activity you should be able to do the following to display a small (empty) dialog in the middle of the screen:

DialogFragment df = new DialogFragment();
df.show(getSupportFragmentManager(), "MyDF");


回答2:

FragmentManager and DialogFragment exists in the compat lib for sdk version 4 and upwards, make sure you import those ones.

User getSupportFragmentManager() to get your FragmentManager for the compat lib.

Show dialog as described in DialogFragment documentation passing compat lib version of `FragmentManager'.

Note that you can also treat the DialogFragment as a Fragment and 'show' it using add(...) or replace(...) as part of a transaction, i.e. you are not restricted to just using show(...)