When activity/fragement is loaded the first, the following sequence is called
Activity.onCreateFragment.onCreate
When screen is rotated, the following sequence is called
Fragment.onCreate(thus,activity==null)Activity.onCreate
To avoid inability to access activity from fragment, it is safer to perform initialization code at Fragment.onActivityCreated.