Skip to content

Instantly share code, notes, and snippets.

@rvprasad
Last active September 17, 2023 04:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rvprasad/7c2075590b035c2fec9a5766dd48b68c to your computer and use it in GitHub Desktop.
Save rvprasad/7c2075590b035c2fec9a5766dd48b68c to your computer and use it in GitHub Desktop.
Initialization Order of Android Components
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.dummy">
<application
android:label="@string/app_name"
tools:targetApi="31"
android:name=".MyApplication">
<provider
android:name=".MyContentProvider"
android:authorities="com.example.dummy.fileprovider"
android:enabled="true"
android:exported="true"/>
<activity
android:name=".MainActivity"
android:exported="true"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
@Test
fun test() {
ActivityScenario.launch(MainActivity::class.java)
}
}
attachBaseContext(MyApplication) called
onCreate(MyContentProvider) called
onCreate(MyApplication) called
attachBaseContext(MainActivity) called
onCreate(MainActivity) called
onStart(MainActivity) called
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Log.d(MyApplication.TAG, "onCreate(MainActivity) called")
Thread.sleep(1000)
}
override fun onStart() {
super.onStart()
Log.d(MyApplication.TAG, "onStart(MainActivity) called")
Thread.sleep(1000)
}
override fun attachBaseContext(newBase: Context?) {
super.attachBaseContext(newBase)
Log.d(MyApplication.TAG, "attachBaseContext(MainActivity) called")
Thread.sleep(1000)
}
}
class MyApplication: Application() {
override fun onCreate() {
super.onCreate()
Log.d(TAG, "onCreate(MyApplication) called")
Thread.sleep(1000)
}
override fun attachBaseContext(base: Context?) {
super.attachBaseContext(base)
Log.d(TAG, "attachBaseContext(MyApplication) called")
Thread.sleep(1000)
}
companion object {
const val TAG = "MyApplication"
}
}
class MyContentProvider : ContentProvider() {
override fun onCreate(): Boolean {
Log.d(MyApplication.TAG, "onCreate(MyContentProvider) called")
Thread.sleep(1000)
return true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment