How to show different Activities in Fragment imple

2019-05-23 04:39发布

问题:

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?