我有Fragment
名为LoginRowFragment
,在其他几个Fragment
或Activity
中实例化。例如:
<?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>
由于这个Fragment
在几个地方被实例化,我正试图找到一种方法来在动作发生时更新Fragment
的内容(在这种情况下,当用户成功验证自己时) ),所以我希望所有Fragment
的实例一次更新,但由于许多Fragment
在识别时被分离,我找不到方法。
这是我到目前为止所尝试过的:
在BroadcastReceiver
内设置Fragment
。但是,当Fragment
被分离时,这不会触发。
为每个实例设置相同的id
。不好的想法,它根本不起作用(基本上我已经尝试过这种愚蠢,因为我已经没有想法了。)
这甚至可能吗?我怎样才能做到这一点?
答案 0 :(得分:1)
如果您一次有一定数量的已知片段(或者可能只有一个,我不知道),您可以将它们作为字段存储在活动中,并在发生更改时直接更新。
然后,将您当前的状态存储在活动中,并在您的片段中检查此状态,onResume()
是一个适合它的好地方:((MyActivity)getActivity()).checkState()
。因此,当片段被唤醒并且onResume()
被调用时,他们将检查他们的状态并采取相应的行动。