Android notes

Activity Lifecycle

A simplified illustration of the activity lifecycle.

Activity Lifecycle

Another simplified illustration of the activity lifecycle.

Activity Lifecycle

To check whether the activity is in the process of finishing, use isFinishing

@Override
protected void onPause() {
  super.onPause();
  if (isFinishing()) {
    Log.i("lifecycle", "Activity is finishing");
  } else {
    Log.i("lifecycle", "Activity is NOT finishing");
  }
}
override fun onPause() {
  super.onPause()
  if (isFinishing) {
    Log.i("lifecycle", "Activity is finishing")
  } else {
    Log.i("lifecycle", "Activity is NOT finishing")
  }
}

To check whether the activity is in the process of being destroyed in order to be recreated with a new configuration, use isChangingConfigurations

@Override
protected void onStop() {
  super.onStop();
  if (isChangingConfigurations()) {
    Log.i("lifecycle", "Activity is changing configurations");
  } else {
    Log.i("lifecycle", "Activity is NOT changing configurations");
  }
}
override fun onStop() {
  super.onStop()
  if (isChangingConfigurations) {
    Log.i("lifecycle", "Activity is changing configurations")
  } else {
    Log.i("lifecycle", "Activity is NOT changing configurations")
  }
}

Fragment Lifecycle

The lifecycle of a fragment (while its activity is running).

Fragment Lifecycle

The effect of the activity lifecycle on the fragment lifecycle.

Fragment Lifecycle

Layout/Views Cheatsheet

Support Library/AndroidX

Support Library location: <ANDROID_SDK_PATH>\extras\android\m2repository\com\android\support\

dependencies {
  implementation 'com.android.support:appcompat-v7:28.0.0'
  implementation 'com.android.support.constraint:constraint-layout:1.1.3'
  androidTestImplementation 'com.android.support.test:runner:1.0.2'
  androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

Note: With the release of Android 9.0 (API level 28) there is a new version of the support library called AndroidX which is part of Jetpack. The AndroidX library contains the existing support library and also includes the latest Jetpack components.

Source

© 2020 | Paul Kim