Android 四大组件——Activity与Fragment的通信方式

https://rainmonth.github.io/posts/A180507.html

Activity与Fragment的通信方式

  • Fragment访问Activity的内容
  • Activity访问Fragment的内容
  • Fragment与Activity共享事件

Fragment访问Activity的内容

​ Fragment通过getActivity()这种方式获取Activity实例后,可以访问Activity中定义的public成员。

Activity访问Fragment的内容

  • 通过FragmentManager的FindFragmentById(int fragmentId)方法,这种情形适用于
    Activity获取其布局文件中的Fragment情况
  • Activity中创建Fragment时,通过Fragment的setArgument(Bundle bundle)方法向Fragment传递参数,然后在Fragment的onCreate()生命周期函数中的调用getArgument()
    方法获取参数

Fragment与Activity共享事件,实现方式(接口回调)

​ Fragment定义接口,然后Fragment所属的Activity实现该接口,Fragment在持有一个
​ 实现接口的Activity实例进行事件的共享。

  1. 在Fragment中定义一个接口,如:

    1
    2
    3
    4
    5
    6
    7
    class TestFragment extends Fragment {
    private OnAnswerSelectedListener listener;

    public interface OnAnswerSelectedListener{
    void onAnswerSelected(String url);
    }
    }
  2. 在TestFragment所属的Activity中实现TestFragment中定义的这个接口
    OnAnswerSelectedListener,如:

    1
    2
    3
    4
    5
    6
    7
    class TestActivity extends Activity implements 
    TestFragment.OnAnswerSelectedListener {

    public void onAnswerSelected(String url) {

    }
    }
  3. 在TestFragment中接受已经实现OnAnswerSelectedListener接口的Activity,如:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    class TestFragment extends Fragment {
    private OnAnswerSelectedListener listener;

    public interface OnAnswerSelectedListener{
    void onAnswerSelected();
    }

    @Override
    public void onAttach(Activity activity) {
    super.onAttach(activity);
    try {
    // 强转要防止异常
    listener = (OnAnswerSelectedListener)activity;
    } catch(ClassCastException e) {
    throw new ClassCastException(activity.toString() +
    " must implement OnAnswerSelectedListener");
    }
    }

    }
  4. 接下来TestFragment就可以和TestActivity共享事件了,如:

    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
    class TestFragment extends Fragment implements 
    ListView.OnListItemClickListener {
    private OnAnswerSelectedListener listener;
    public interface OnAnswerSelectedListener{
    void onAnswerSelected();
    }

    @Override
    public void onAttach(Activity activity) {
    super.onAttach(activity);
    try {
    // 强转要防止异常
    listener = (OnAnswerSelectedListener)activity;
    } catch(ClassCastException e) {
    throw new ClassCastException(activity.toString() +
    " must implement OnAnswerSelectedListener");
    }
    }
    @Override
    public void onListItemClick(ListView l,View v,int position,long id){
    // 生成参数
    Uri noteUri =ContentUris.withAppendedId(ArticleColumns
    .CONTENT_URI,id);
    // 将事件和参数传递给所属的Activity
    listener.onArticleSelected(noteUri); // TestActivity 中处理该事件
    }
    }
1
2
3
4
5
6
7
8
9
Activity各声明周期函数执行的具体时机

onCreate()

```java
ActivityThread.performLaunchActivity
Instrumentation.callActivityOnCreate
Activity.performCreate
Activity.onCreate

onStart()

1
2
3
4
5
6
7
8
ActivityThread.handleResumeActivity
ActivityThread.performResumeActivity
-> r.activity.performResume(r.startsNotResumed, reason);
Activity.performResume
Activity.performRestart
Activity.performStart
Instrumentation.callActivityOnStart
activity.onStart

onResume()

1
2
3
4
5
6
ActivityThread.handleResumeActivity
ActivityThread.performResumeActivity
-> r.activity.performResume(r.startsNotResumed, reason)
Activity.performResume()
Instrumentation.callActivityOnResume
Activity.onResume()

onPause()

1
2
3
4
5
6
ActivityThread.handlePauseActivity
ActivityThread.performPauseActivity
ActivityThread.performPauseActivityIfNeeded
Instrumentation.callActivityOnPause
Activity.performPause
Activity.onPause

onStop()

1
2
3
4
5
6
7
ActivityThread.handleStopActivity
ActivityThread.performStopActivityInner
ActivityThread.performPauseActivityIfNeeded
ActivityThread.callActivityOnStop
Activity.performStop
Instrumentation.callActivityOnStop
Activity.onStop

onDestroy

1
2
3
4
5
ActivityThread.handleDestroyActivity
ActivityThread.performDestoryActivity
Instrumentation.callActivityOnDestroy
Activity.performDestroy
Activity.onDestroy