Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I show a DialogFragment using compatibility package?

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. :)

like image 921
kunny Avatar asked Jul 19 '11 12:07

kunny


2 Answers

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");
like image 172
David Snabel-Caunt Avatar answered Nov 03 '22 21:11

David Snabel-Caunt


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(...)

like image 36
PJL Avatar answered Nov 03 '22 19:11

PJL