How to show different Activities in Fragment implementation?

旧时模样 提交于 2020-02-02 11:06:46

问题


It is my first time using android fragment. I am following this tutorial to implement a fragment.

Everything is fine with this tutorial, I successfully get the result like below:

In the tutorial, the DetailsFragment simply shows a TextView containing the text of the currently selected item. That's the right part shows just some texts.

My question is how to show different activities on the right side instead of text views.

What I mean is illustrated in the following image, for example, the area of "1" in the image is an activity. How to show different activities when a list item on the left hand side has selected?


回答1:


You do not show an activity, you show a fragment. Implement the Fragment class instead of the Activity class. Then you build your View just as you would in an Activity. Remember that for instances when you need access to an activity the Fragment class has the convenient getActivity() method.




回答2:


Use FragmentManager.beginTransaction() to start FragmentTransaction. With that operation you can hide and show new Fragments. It is also managed with the android history stack.

https://developer.android.com/reference/android/app/FragmentManager.html#beginTransaction() https://developer.android.com/reference/android/app/FragmentTransaction.html

And here is some code: Android Honeycomb: How to change Fragments in a FrameLayout, without re-creating them?



来源:https://stackoverflow.com/questions/6693649/how-to-show-different-activities-in-fragment-implementation

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