Skip to content

Instantly share code, notes, and snippets.

@kaushikgopal
Last active February 2, 2022 07:28
Show Gist options
  • Star 80 You must be signed in to star a gist
  • Fork 11 You must be signed in to fork a gist
  • Save kaushikgopal/5c1b029798b73c73193d to your computer and use it in GitHub Desktop.
Save kaushikgopal/5c1b029798b73c73193d 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)
@pepos
Copy link

pepos commented Nov 17, 2017

Very nice document, I only have a concern, I would not recommend the following or at least I would add a warning:

View

onAttachedToWindow

  • RxJava CompositeSubscription.add

onDetachedFromWindow

  • RxJava CompositeSubscription.unsubscribe

When the app is pushed to the background, the view won't be detached from the window. So you need to be careful not to do some UI update like open new activity in this callback, because it is not guarantee that the app is in the foreground.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment