Fragment和Activity通信
发布时间 :
字数:846
阅读 :
Activity与Fragment通信
1. 广播
自定义BroadcastReceiver, 定义IntentFilter的action
动态注册并传参
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| public class DataBroadcastReceiver extends BroadcastReceiver{ @Override public void onReceiver(Context context, Intent intent){ Object data = intent.getParcelExtra("data"); } }
private DataBroadcastReceiver dataBroadcastReceiver;
@Override protected void onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction("com.broadcast.data"); dataBroadcastReceiver = new DataBroadcastReceiver(); registerReceiver(dataBroadcastReceiver,intentFilter); return super.onCreateView(inflater, container, savedInstanceState); }
@Override protected void onDestroyView() { super.onDestroyView(); unregisterReceiver(dataBroadcastReceiver); }
public void sendData(Object data){ Intent intent = new Intent(); intent.setAction("com.broadcast.data"); intent.putExtra("data",data); sendBroadcast(intent); }
|
2. 对象调用 public 方法
1 2 3 4 5 6 7 8 9
| public class TestActivity extends AppCompactActivity{ private TestFragment testFragment; public void test(){ testFragment = new TestFragment(); testFragment.doTest(); } }
|
1 2 3 4 5 6 7
| public class TestFragment extends Fragment{ private TestActivity activity; public void doTest(){ Log.d(TAG,"收到消息"); } }
|
3. 接口
1 2 3 4 5 6 7 8 9 10 11
| public class TestActivity extends Activity{
private ITest iTest;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); iTest = new TestFragment(); iTest.doTest(); } }
|
1 2 3 4 5 6 7 8 9 10 11 12
| public class TestFragment extends Fragment implements ITest{
@Override public void doTest() { Log.i(TAG, "doTest: "); } }
public interface ITest{ void doTest(); }
|
4. 观察者
这里模拟一个场景,用户在 Activity 里登录状态发生了改变,Fragment 展示退出登录的 UI。我真实项目里也是采用的观察者模式管理登录状态,因为登录状态的改变牵连到一系列的改变。
1 2 3 4 5 6 7 8 9 10
| public class TestActivity extends Activity {
final int LOGIN_OUT = 0;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); LoginManager.loginStatusChanged(LOGIN_OUT); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
public class TestFragment extends Fragment implements LoginObserver { @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
LoginManager.registerLoginObserver(this); return super.onCreateView(inflater, container, savedInstanceState); }
@Override public void onDestroy() { super.onDestroy();
LoginManager.unregisterLoginObserver(this); }
@Override public void loginStatusChange(int status) { Log.i(TAG, "loginStatusChange: "+status); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| public class LoginManager {
private static ArrayList<LoginObserver> loginObserverList = new ArrayList<>();
public static void registerLoginObserver(LoginObserver loginObserver){ loginObserverList.add(loginObserver); }
public static void unregisterLoginObserver(LoginObserver loginObserver){ loginObserverList.remove(loginObserver); }
public static void loginStatusChanged(int loginstatus){ for (LoginObserver observer:loginObserverList){ if (observer!=null){ observer.loginStatusChange(loginstatus); } } } }
|
1 2 3 4
| public interface LoginObserver{
void loginStatusChange(int status); }
|
5. EventBus
看官方说明
Fragment与Activity通信
1. 广播
同上
2. 对象调用 public 方法
1 2 3 4 5 6
| public class TestActivity extends Activity { public void doTest(){ Log.i(TAG, "doTest: "); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| public class TestFragment extends Fragment {
private TestActivity activity; @Override public void onAttach(Context context) { super.onAttach(context); if (getActivity() instanceof TestActivity){ activity = getActivity(); } }
@Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { return super.onCreateView(inflater, container, savedInstanceState); }
void test(){ activity.doTest(); } }
|
3. 接口
1 2 3 4 5 6 7
| public class TestActivity extends Activity implements ITest{ @Override public void doTest() { Log.i(TAG, "doTest: "); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| public class TestFragment extends Fragment {
private ITest iTest; @Override public void onAttach(Context context) { super.onAttach(context); if (context instanceof ITest){ iTest = (ITest) context; } }
@Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { return super.onCreateView(inflater, container, savedInstanceState); }
void test(){ iTest.doTest(); } }
|
1 2 3 4
| public interface ITest{ void doTest(); }
|
4. 观察者
同1.4,只不过这次是 Fragment 改变用户登录状态,Activiy 观察事件而已。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| public class TestActivity extends Activity implements LoginObserver {
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); LoginManager.registerLoginObserver(this); }
@Override protected void onDestroy() { super.onDestroy(); LoginManager.unregisterLoginObserver(this); }
@Override public void loginStatusChange(int status) {
} }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| public class TestFragment extends Fragment {
final int LOGIN_OUT = 0;
@Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { return super.onCreateView(inflater, container, savedInstanceState); }
void doTest(){ LoginManager.loginStatusChanged(LOGIN_OUT); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| public class LoginManager {
private static ArrayList<LoginObserver> loginObserverList = new ArrayList<>();
public static void registerLoginObserver(LoginObserver loginObserver){ loginObserverList.add(loginObserver); }
public static void unregisterLoginObserver(LoginObserver loginObserver){ loginObserverList.remove(loginObserver); }
public static void loginStatusChanged(int loginstatus){ for (LoginObserver observer:loginObserverList){ if (observer!=null){ observer.loginStatusChange(loginstatus); } } } }
|
1 2 3 4 5
| public interface LoginObserver{
void loginStatusChange(int status); }
|
5. EventBus
同上
转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 nathanwriting@126.com
文章标题:Fragment和Activity通信
字数:846
本文作者:Nathaniel
发布时间:2021-02-24, 11:40:28
最后更新:2023-11-06, 22:59:18
原始链接:http://example.com/2021/02/24/fragment-data-activity/
版权声明: "署名-非商用-相同方式共享 4.0" 转载请保留原文链接及作者。