在Activity onStart()之前或之后调用片段的初始回调吗?

时间:2017-09-16 18:46:02

标签: android android-lifecycle

https://github.com/xxv/android-lifecycle显示在活动onStart之前调用onAttach,onCreate,CreateView和onActivityCreated上的片段。但是,我尝试记录这个:

   public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.e("TRACE", "MainActivity onCreate");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Fragment firstFragment = FirstFragment.newInstance();
        getSupportFragmentManager().beginTransaction().replace(R.id.content, firstFragment).commit();
    }

    @Override
    public void onStart() {
        Log.e("TRACE", "MainActivity onStart");
        super.onStart();
    }

FirstFragment看起来像这样:

public class FirstFragment extends Fragment {

    public FirstFragment() {
        // Required empty public constructor
    }

    public static FirstFragment newInstance() {
        FirstFragment fragment = new FirstFragment();
        return fragment;
    }

    @Override
    public void onAttach(Context context) {
        Log.d("TRACE", "FirstFragment onAttach");
        super.onAttach(context);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        Log.d("TRACE", "FirstFragment onCreate");
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        Log.d("TRACE", "FirstFragment onCreateView");
        return inflater.inflate(R.layout.fragment_first, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        Log.d("TRACE", "FirstFragment onActivityCreated");
        super.onActivityCreated(savedInstanceState);
    }

它打印了这个:

  1. MainActivity onCreate
  2. MainActivity onStart
  3. FirstFragment onAttach
  4. FirstFragment onCreate
  5. FirstFragment onCreateView
  6. FirstFragment onActivityCreated
  7. MainActivity onResume

1 个答案:

答案 0 :(得分:1)

这取决于Fragment添加Activity

将片段添加到活动有两种常用方法:通过XML中的<fragment>标记或通过Java执行的FragmentTransaction

如果您使用与此类似的活动布局,而不是提供R.id.content视图并使用FragmentTransaction,则可以创建FirstFragment

<?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"
    android:orientation="vertical">

    <fragment
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.example.stackoverflow.FirstFragment"/>

</LinearLayout>

你会在日志中看到这个:

E/TRACE   ( 6094): MainActivity onCreate
D/TRACE   ( 6094): FirstFragment onAttach
D/TRACE   ( 6094): FirstFragment onCreate
D/TRACE   ( 6094): FirstFragment onCreateView
E/TRACE   ( 6094): MainActivity onStart
D/TRACE   ( 6094): FirstFragment onActivityCreated
E/TRACE   ( 6094): MainActivity onResume

至于您在使用FragmentTransaction时按顺序查看邮件的原因,可归结为事务是异步;一旦你致电commit(),我们无法保证会立即执行。在您的情况下,系统实际上并不会在您的活动onStart()之后执行它。