Well, i got a simple <FrameLayout>
:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/FragmentContainer" android:layout_width="fill_parent" android:layout_height="fill_parent" />
Then in my code, i added a Fragment to it:
FragClass aFrag = new FragClass(); getSupportFragmentManager().beginTransaction() .replace(R.id.FragmentContainer, aFrag).commit();
And somewhere else in my code, i want to get that FragClass (extends Fragment)
object from the ID R.id.FragmentContainer
.
i have tried
((ViewGroup) findViewById(R.id.FragmentContainer)).getChildAt(0)
or
((FrameLayout) findViewById(R.id.FragmentContainer)).getChildAt(0)
but they are returning the View
, instead of the Fragment
attached to it.
i know i can keep the variable aFrag
somewhere, so i do not need to find it again. But i believe there should be a way to retieve it.
and then you can use the FragmentManager to create a FragmentTransaction which allows us to add fragments to the FrameLayout at runtime: // Begin the transaction FragmentTransaction ft = getSupportFragmentManager(). beginTransaction(); // Replace the contents of the container with the new fragment ft. replace(R.
FrameLayout is designed to block out an area on the screen to display a single item. Generally, FrameLayout should be used to hold a single child view, because it can be difficult to organize child views in a way that's scalable to different screen sizes without the children overlapping each other.
Add a fragment to an activity You can add your fragment to the activity's view hierarchy either by defining the fragment in your activity's layout file or by defining a fragment container in your activity's layout file and then programmatically adding the fragment from within your activity.
Show activity on this post. A framelayout, Relative View and a few others represents a view in android and is extended from viewgroup. A Fragment is a an an Object that is used to represent a portion of a user interface and is usually hosted in an activity. A fragment has a viewgroup which you can assign an XML layout.
Let me wrap it up by a full answer :)
In this case, the dynamically added Fragment
uses the ID of the container View
(ViewGroup
).
ref: http://developer.android.com/guide/components/fragments.html#Adding
Note: Each fragment requires a unique identifier that the system can use to restore the fragment if the activity is restarted (and which you can use to capture the fragment to perform transactions, such as remove it). There are three ways to provide an ID for a fragment:
- Supply the android:id attribute with a unique ID.
- Supply the android:tag attribute with a unique string.
- If you provide neither of the previous two, the system uses the ID of the container view.
It is because it's a Fragment
afterall. We have to use getSupportFragmentManager().findFragmentById()
to retrieve it, which returns a Fragment
, instead of findViewById()
which returns a View
.
So the answer to this problem would be:
((aFrag) getSupportFragmentManager().findFragmentById(R.id.FragmentContainer))
Thanks to @Luksprog.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With