Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jkreiser/cd314e5d0f05b69d9c5d4bf7ec3df0a9 to your computer and use it in GitHub Desktop.
Save jkreiser/cd314e5d0f05b69d9c5d4bf7ec3df0a9 to your computer and use it in GitHub Desktop.
Notes on opportune moments to do "stuff" in the Android Lifecycle
  • In general you want to try and put things in onStart and onStop for logical start and stops.

Activity

onCreate

  • Dagger inject self into graph
  • setContentView(R.layout.xxx)
  • Butterknife.bind(this)
  • RxJava CompositeSubscription.add (if NON UI related work being done)
  • realm = Realm.getDefaultInstance();

onStart

onResume

  • RxJava CompositeSubscription.add (if ui related work being done)

onPause

  • RxJava CompositeSubscription.clear (if ui related work being done)

onStop

  • EventBus unregister

onDestroy

  • Dagger "Activity" scoped graph destruction
  • RxJava CompositeSubscription.clear (if NON UI related work being done)
  • realm.close();

Fragment (DialogFragment etc.)

onAttach(Context) (prev.Activity)

  • Dagger inject self into graph
  • setCallbackOrListener(activity) - when you want to communicate back to the activity

Warning: lifecycle callpoint is subtly different now. onAttach(Context) is called later than onAttach(Activity) and so if you expected the injection to have happened before certain callbacks like onViewCreated this is not the case (e.g. API 19)

onCreateView

  • view = Inflate.inflation
  • ButterKnife.bind(this, view);
  • realm = Realm.getDefaultInstance();

onStart

  • EventBus register

onResume

  • RxJava CompositeSubscription.add (if ui related work being done)

onPause

  • RxJava CompositeSubscription.clear (if ui related work being done)

onStop

  • EventBus unregister

onDestroy

  • LeakCanary MyApp.getRefWatcher().watch(this);

onDestroyView

  • ButterKnife.unbind(this);
  • realm.close();

View

onFinishInflate

  • Butterknife.bind(this)

onAttachedToWindow

  • RxJava CompositeSubscription.add

onDetachedFromWindow

  • RxJava CompositeSubscription.clear

Application

  • LeakCanary Refwatcher = LeakCanary.install(this);
  • Dagger ObjectGraph.create

ViewHolder (for RecyclerViews, ListViews etc.)

ViewHolder (Constructor)

  • Butterknife.bind(this. itemView)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment