Reputation: 13761
I have a Fragment
called LoginRowFragment
which is instantiated in several other Fragment
s or Activity
s. For instance:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/loginrow"
class="com.mydomain.myproject.LoginRowFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
As this Fragment
is instantiated in several places, I'm trying to find a way to update the content of that Fragment
when an action happens (in this case, when an user successfully authenticates himself), so I want all instances of that Fragment
being updated at once, but since many of those Fragment
s are detached at the moment of identification, I can't find out a way.
This is what I have have tried so far:
Setting a BroadcastReceiver
inside the Fragment
. This doesn't fire when the Fragment
is detached, though.
Setting the same id
to every instance. Bad idea, it doesn't work at all (basically I've tried this silliness because I've run out of ideas).
Is that even possible? How can I achieve that?
Upvotes: 0
Views: 40
Reputation: 6462
If you have a constant known amount of fragments active at a time (or maybe only one, I don't know), you can store them in activity as fields and update directly when a change occures.
Then, store your current state in activity and implement a check of this state in your fragment, onResume()
is a good place for it: ((MyActivity)getActivity()).checkState()
. Therefore, when fragments will wake up and onResume()
gets called they'll check their state and act accordingly.
Upvotes: 1