Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get Fragment from View?

I added some Fragment into a TableLayout and I want to manage them from my container Activity, so I used this:

Fragment fragment = (Fragment) tableLayout.getChildAt(i); 

but getChildAt(int) returns a View and a View could NOT cast to Fragment

like image 792
golkarm Avatar asked Jul 31 '15 10:07

golkarm


People also ask

Is fragment a view?

Because an Android fragment is not a view, adding it to an activity looks somewhat different than adding a view (e.g. TextView ). A fragment is added to a ViewGroup inside the activity. The fragment's view is displayed inside this ViewGroup .

Can a fragment have its own view?

Fragments cannot live on their own--they must be hosted by an activity or another fragment. The fragment's view hierarchy becomes part of, or attaches to, the host's view hierarchy.

How do I get the currently displayed fragment?

To get the current fragment that's active in your Android Activity class, you need to use the supportFragmentManager object. The supportFragmentManager has findFragmentById() and findFragmentByTag() methods that you can use to get a fragment instance.

How do I get the current fragment from supportFragmentManager?

FragmentManager fragMgr = getSupportFragmentManager(); FragmentTransaction fragTrans = fragMgr. beginTransaction(); MyFragment myFragment = new MyFragment(); //my custom fragment fragTrans. replace(android. R.


1 Answers

I don't understand why people are down-voting your question. Fragments can be very confusing at times, especially for beginners. To understand your problem, you must learn what is a Fragment and how they are used.

To start with, a View is something that has an existence on the screen. Examples include: TextView, EditText, Button, etc. They are placed inside "layouts" written in Xml or Java/Kotlin. These layouts are shown using an Activity.

Now, a Fragment is not a View. It does not have any existence on the screen at all. Instead, it's a class that simply manages a "layout" — kinda similar to an Activity. If you need the View returned by your Fragment's onCreateView(), you can directly use findViewById() within your Activity.

If you need a reference to your Fragment, there are two possible ways of doing this:

1) If you added the Fragment programmatically like this

getFragmentManager()     .beginTransaction()     .replace(R.id.fragment_container_viewgroup, myFragment, FRAGMENT_TAG)     .commit(); 

You can use:

MyFragment myFragment = (MyFragment) getFragmentManager().findFragmentByTag(FRAGMENT_TAG); 

2) If you added the Fragment inside an XML layout like this:

<fragment android:name="com.example.android.fragments.HeadlinesFragment"     android:id="@+id/fragmentContainer"     android:layout_weight="1"     android:layout_width="0dp"     android:layout_height="match_parent" /> 

You can use this:

getFragmentManager().findFragmentById(R.id.fragmentContainer); 

Basically, each Activity has a FragmentManager class that maintains all the active Fragments, and there are two ways of finding them: Using a unique TAG that you pass while showing a fragment, or passing the container view-ID where the fragment was added.

like image 162
Saket Avatar answered Oct 20 '22 01:10

Saket