Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save artsuhov/4e5da11d57bd4759892fa6cd1553479c to your computer and use it in GitHub Desktop.
Save artsuhov/4e5da11d57bd4759892fa6cd1553479c 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);

onStart

  • EventBus register
  • realm = Realm.getDefaultInstance();

onResume

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

onPause

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

onStop

  • EventBus unregister
  • realm.close();

onDestroy

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

onDestroyView

  • ButterKnife.unbind(this);

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