I have got a project which was developed by another person. In that there is a fragment but I cannot find who is instantiating it. Is it possible that a fragment could be created by the main activity through layout xmls? That is, without instantiating it in Java code using" new Fragment", is it possible to instantiate it through some xml or something? Because I have checked the Fragment constructor usage and it also doesn't show any other class calling it. But when I debug the code, the fragment does get called. If its possible, how can I pass arguments from an activity to this fragment? Because if its created using "Fragment f = new Fragment" I could use the setArguments method or even pass it through constructor. But on a situation like this, how can I pass values from an activity to this fragment? Please advice.
Asked
Active
Viewed 857 times
1

AnOldSoul
- 4,017
- 12
- 57
- 118
-
Yes. It is possible with layout
. You will find this [link](https://guides.codepath.com/android/Creating-and-Using-Fragments) useful. – Harish Elumalai Mar 10 '16 at 23:32 -
Yes it's probably in xml. This is how you can get the fragment if it is. http://stackoverflow.com/questions/8532462/how-to-get-the-fragment-instance-from-the-fragmentactivity – ootinii Mar 10 '16 at 23:35
-
Actually I don't need to get the fragment instance. I'm fine with the way its loading. But how can I pass a parameter to this fragment if its getting created the way it seems to? In order to launch this fragment I am calling to the MainActivity class and this fragment loads eventually. Is there a way I can pass a parameter to this fragment through that call I make to MainActivity? – AnOldSoul Mar 10 '16 at 23:38
3 Answers
0
Yes, it's possible, see docs. For instance, from that page:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<fragment android:name="com.example.android.fragments.HeadlinesFragment"
android:id="@+id/headlines_fragment"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
<fragment android:name="com.example.android.fragments.ArticleFragment"
android:id="@+id/article_fragment"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent" />
</LinearLayout>

Francesc
- 25,014
- 10
- 66
- 84
-
-
You can't at instantiation, but you can add APIs to customize the fragment after instantiation. Alternatively, remove it from the layout and create it in code in your activity so you can customize the way the fragment is constructed. – Francesc Mar 10 '16 at 23:34
-
I need to load this fragment on 1 occasions. At one the UI should look different and at the other occasion it should look in another way. So I have to do this based on the parameter passed to it. How do I pass a parameter to this fragment? :( – AnOldSoul Mar 10 '16 at 23:35
-
Create a factory method for your fragment and pass an argument that you use to determine how the fragment should behave. See this link for an example: http://www.androiddesignpatterns.com/2012/05/using-newinstance-to-instantiate.html – Francesc Mar 10 '16 at 23:37
0
Is it possible that a fragment could be created by the main activity through layout xmls?
Yes, this is a static Fragment
. If you check the layout files, you should see a <fragment />
tag:
<fragment
android:id="@+id/example_fragment"
android:name="com.example.staticfragexample.MyFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
There will then be a corresponding MyFragment
class. To access it, you would use getFragmentManager().findFragmentById(R.id.example_fragment);

PPartisan
- 8,173
- 4
- 29
- 48
-
Actually I don't need to get the fragment instance. I'm fine with the way its loading. But how can I pass a parameter to this fragment if its getting created the way it seems to? In order to launch this fragment I am calling to the MainActivity class and this fragment loads eventually. Is there a way I can pass a parameter to this fragment through that call I make to MainActivity? – AnOldSoul Mar 10 '16 at 23:38
-
@mayooran Not as it stands, no. You could however potentially update any UI elements via `((MyFragment)getFragmentManager().findFragmentById(R.id.example_fragment)).getView()`, which will return the `Fragment`'s root view so long as you call it after `onCreate()` of your `Activity`. The simplest answer though is probably to rewrite the code to use dynamic fragments instead. – PPartisan Mar 10 '16 at 23:43
0
You are saying that the fragments are not instantiated through java code. That means they must have been declared in xml and are instantiated as soon as the activity gets created like this -
<fragment android:name="com.test.SampleFragment"
android:id="@+id/sample_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
In this case you can't use setArguments()
, but instead use some other mechanism to send data to the fragments .

Shadab Ansari
- 7,022
- 2
- 27
- 45