Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to pass arguments to a fragment after it's been added to an activity?

I know that when you're first instantiating a fragment you can pass arguments using setArguments(Bundle) and retrieve them in the fragment using getArguments().

However, in my app I have fragments that will be detached and attached several times after they've been added to an activity. On re-attach, I may need to pass a fragment an argument to modify its content prior to reattaching it. I can use setArguments the first time I display the fragment, but on subsequent occasions that won't work. The savedInstanceState will not work in this case as I won't know the value of the argument prior to detaching the fragment.

I know I could just implement a method that I would call before attaching the fragment that would set an argument, but it just seems like this is something that might already be in the API and I'm just not seeing it.

Is there something built-in that will allow me to do this, or will I have to implement this on my own? For the record, I am using the support package (v4).

Many thanks!

like image 774
Jay Lamont Avatar asked Mar 07 '12 21:03

Jay Lamont


People also ask

Do arguments on fragments survive after a configuration change?

Fragments suffer the same process. After a configuration change, all the objects are going to be created again. The old objects will be cannon fodder for the Garbage Collector and the new ones won't keep the state the others have.

What is the relationship between a fragment and an activity?

Activity is an application component that gives a user interface where the user can interact. The fragment is only part of an activity, it basically contributes its UI to that activity. Fragment is dependent on activity. It can't exist independently.

Can you use a fragment in more than one activity?

You can use a Fragment in more than one Activity . One Activity can have multiple fragments. After you define a Fragment in a Kotlin class, the fragment is automatically added to the activity_main.


2 Answers

Yes, if you have called setArguments(bundle) before your fragment becomes active. Then your fragment from there after has a bundle that you can update. To avoid your issue you must update the original bundle and must not invoke setArguments a second time. So following your initial fragment construction, modify fragment arguments with code like

frg.getArguments().putString("someKey", "someValue"); 

The arguments will then be available in your fragment and will be persisted and restored during orientation changes and such.

Note this method is also useful when the fragment is being created via xml in a layout. Ordinarily one would not be able to set arguments on such a fragment; the way to avoid this restriction is to create a no argument constructor which creates the argument bundle like so:

public MyFragment() {     this.setArguments(new Bundle()); } 

Later somewhere in your activity's onCreate method you would then do:

FragmentManager mgr = this.getSupportFragmentManager(); Fragment frg = mgr.findFragmentById(R.id.gl_frgMyFragment); Bundle bdl = frg.getArguments(); bdl.putSerializable(MyFragment.ATTR_SOMEATTR, someData); 

This places data into the argument bundle, which will then be available to code in your fragment.

like image 137
user3978046 Avatar answered Sep 17 '22 21:09

user3978046


You can just expose a method on your fragment that set whatever you want to pass to it. To call it you can e.g. retrieve the fragment from the backstack by tag or keep an instance reference around from wherever you are calling it from.

This works nicely for me although you need to be defensive in terms of null checks and such as well as aware of the lifecyle your fragment goes through when you attach it or restart it.

From what I can tell there is nothing in the API...

Update: This is still true and works just fine. I found that once this is more complex it is much cleaner and easier to use something like the Otto eventbus. Highly recommended imho.

like image 20
Manfred Moser Avatar answered Sep 20 '22 21:09

Manfred Moser