How to show fragment as a dialog?
I have developed application in which I want to display Fragment as a dialog,
I used Tabs and Fragment in my application, I have just one activity and I replace the fragment as I need,
If we used activity then we declare "android:theme="@android:style/Theme.Dialog" in Manifest file to display activity as dialog, same thing I want to do for Fragment
We can show a Fragment as a dialog using two means , but a single way.
Explanation:
Way:
Extend class DialogFragment and override any one of two methods:
onCreateView() OR
onCreateDialog().
Diff. between these two:
Overriding onCreateView () will let you show a Fragment as dialog and you can make the Title text customized.
On the other hand, overriding onCreateDialog (), you can again show a fragment as dialog and here you can customize the entire dialog fragment. Means, you can inflate any view to show as dialog.
If you need any source code explaining the above text, let me know.
Note:
Using DialogFragment has a disadvantage. It doesn't handle screen orientation. And crashes the app.
So, you need to use setRetainInstance() inside onCreate() of DialogFragment class.
Your fragment class should extend DialogFragment rather than fragment.
Check out the docs: http://developer.android.com/reference/android/app/DialogFragment.html
Just use a DialogFragment. It is the intended fragment subclass for this use.. http://developer.android.com/reference/android/app/DialogFragment.html
链接地址: http://www.djcxy.com/p/79054.html上一篇: 重新加载属于ViewPager的片段
下一篇: 如何将片段显示为对话框?