简体   繁体   中英

What gets called before oncreate()

I have below code and want to know what gets called before onCreate() because it shows blank screen before the activity(Tabbed activity) gets called. What can be done for this to avoid showing blank screen ?

I can't even use onAttach as my Class is extending AppCompatActivity and implements OnMapReadyCallback , PlaceSelectionListener .

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_tabbed);
  TabHost tab = (TabHost) findViewById(R.id.tabHost);
  tab.setup();

  TabHost.TabSpec spec1 = tab.newTabSpec("Search");
  spec1.setIndicator("Search");
  spec1.setContent(R.id.layout1);
  tab.addTab(spec1);

  TabHost.TabSpec spec2 = tab.newTabSpec("Settings");
  spec2.setIndicator("Settings");
  spec2.setContent(R.id.layout2);
  tab.addTab(spec2);

  SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
  mapFragment.getMapAsync(this);
  locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
  provider = locationManager.getBestProvider(new Criteria(), false);

  if (android.os.Build.VERSION.SDK_INT > 9) {
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
  }
}

in case of activity is created the following function is called:

onCreate()

onStart()   

onResume()

onPause()

onStop()

onDestroy()

onRestart()

and when fragment is made in an activity then:

onAttach()

onCreate() 

onCreateView() 

onActivityCreated()

 onCreateView() 

onStart()

onResume()

onPause() 

onStop()

onDestroyView()

onDestroy()

To Avoid the blank screen, you can use below code. It works !!

<style name="Theme.Transparent" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowActionBar">false</item>
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowIsTranslucent">true</item>
</style>
and use it with your activity in AndroidManifest as:

<activity android:name=".ActivitySplash" android:theme="@style/Theme.Transparent">

To investigate slow loads and white screens, you should use Android's TraceView feature: MethodTracing .

If you have a class extending Application in your project add Debug.startMethodTracing() as the first line in its onCreate method, if you don't have an Application class, add this line as the first line of your Activity 's onCreate .

Later, in your activity's onResume, or even later if you want, you can call Debug.stopMethodTracing() to stop tracing.

You can view the Trace files created in Studio to get a sense of which methods took long and why.

See docs here: https://developer.android.com/studio/profile/traceview.html

Other profiling tools by Android: https://developer.android.com/studio/profile/android-monitor.html#monitors

In Activity , attachBaseContext is called before onCreate . You can create a Log to test this.

Read more in AppCompatActivity .

Or you can see all methods is called in the order in AppCompatActivity.java

在此处输入图片说明

Nothing is called in the activity before onCreate,look the image below

BUT before any activity is created, an applicaton instance is instanciated:

https://developer.android.com/reference/android/app/Application.html

在此处输入图片说明

As ΦXocę 웃 Пepeúpa ツsays, there is no method called before onCreate() . You can look at the following diagram taken from Complete Android Fragment & Activity Lifecycle :

在此处输入图片说明

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