简体   繁体   中英

Do the initial callbacks of a fragment get called before or after Activity onStart()?

https://github.com/xxv/android-lifecycle shows that fragment onAttach, onCreate, on CreateView, and onActivityCreated are invoked prior to activity onStart. However, I tried logging this:

   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 looks like this:

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);
    }

And it printed this:

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

It depends on how the Fragment is added to the Activity .

There are two common ways to add a fragment to an activity: via a <fragment> tag in XML or via a FragmentTransaction executed in Java.

If, instead of providing an R.id.content view and using a FragmentTransaction , you create your FirstFragment by using an activity layout like this one:

<?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>

You'll see this in the logs:

 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 

As for why you're seeing the messages in the order you see them when using a FragmentTransaction , it comes down to the fact that the transaction is asynchronous ; there's no guarantee that it will be executed as soon as you call commit() . In your case, the system doesn't actually get around to executing it until after your activity's onStart() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM