1

If we turn on "Don't keep activites" and "No background processes" in Developer options, we can produce the following behaviour easily.

  • Activity.super.onCreate() will trigger Fragment.onCreate()
  • Activity will only initialize data structure after Activity.super.onCreate()
  • If Fragment.onCreate() is accessing data structure in Activity, crash will happen as data structure is not being initialized yet.

This behaviour is described in Why Fragment's onCreate() is sometimes called prior to Activity's onCreate()?

We just change our code from


Accessing Bundle after Activity.super.onCreate

public class MyActivity extends AppCompatActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        final Bundle bundle = this.getIntent().getExtras();
        this.dataStructure = bundle.getParcelable(...);
    }
}

to


Accessing Bundle before Activity.super.onCreate

public class MyActivity extends AppCompatActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        final Bundle bundle = this.getIntent().getExtras();
        this.dataStructure = bundle.getParcelable(...);

        super.onCreate(savedInstanceState);
    }
}

We have done intensive testing and it works fine.

However, under Firebase Crashlytics, we saw very minor edge cases, getIntent().getExtras() is returning null sometimes?

I was wondering, is it caused by the fact that I am accessing getIntent().getExtras() before Activity.super.onCreate(), or that might due to other reasons?

Cheok Yan Cheng
  • 47,586
  • 132
  • 466
  • 875

0 Answers0