Nikhil
Nikhil

Reputation: 167

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

Upvotes: 1

Views: 3881

Answers (6)

albertkhang
albertkhang

Reputation: 701

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

enter image description here

Upvotes: 1

marmor
marmor

Reputation: 28179

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

Upvotes: 1

ישו אוהב אותך
ישו אוהב אותך

Reputation: 29783

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:

enter image description here

Upvotes: 0

hiashutoshsingh
hiashutoshsingh

Reputation: 1020

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()

Upvotes: 2

Nikhil
Nikhil

Reputation: 167

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

Upvotes: 1

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

enter image description here

Upvotes: 0

Related Questions