nKn
nKn

Reputation: 13761

Updating all instances of a Fragment at once

I have a Fragment called LoginRowFragment which is instantiated in several other Fragments or Activitys. 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 Fragments are detached at the moment of identification, I can't find out a way.

This is what I have have tried so far:

Is that even possible? How can I achieve that?

Upvotes: 0

Views: 40

Answers (1)

Dmide
Dmide

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

Related Questions