How can I show a DialogFragment using compatibility package?

倖福魔咒の 提交于 2019-12-08 22:15:43

问题


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



来源:https://stackoverflow.com/questions/6746978/how-can-i-show-a-dialogfragment-using-compatibility-package

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