konmik / nucleus

Nucleus is an Android library, which utilizes the Model-View-Presenter pattern to properly connect background tasks with visual parts of an application.
MIT License
1.98k stars 253 forks source link

Calling setPresenter in OnCreate #150

Closed fredagsfys closed 6 years ago

fredagsfys commented 6 years ago

Hi, looking at this example: https://github.com/konmik/nucleus/blob/master/nucleus-example-real-life/src/main/java/nucleus/example/ui/base/BaseFragment.java#L20

getActivity() is being called in onCreate() which doesn't guarantee that the fragment is attached to its host yet. We have experienced it the hard way in our application 💃

We are utilizing a BaseFragment were I guess we have to either use isAdded() check or put it elsewhere like at onActivityCreated() and then in our Fragment call getPresenter() either in onStart() or onActivityCreated() to not get a crash.

Was this written on purpose to be as simple as possible or did i miss something?

konmik commented 6 years ago

Hi! I never had the issue you're talking about. Here are the docs: https://developer.android.com/guide/components/fragments.html

It says that onAttach happens before onCreate, so everything should be OK. Do you have a reproducible example?

fredagsfys commented 6 years ago

Issue is resolved and has nothing to do with the explained issue above. We traced it down to textChanged() using rxbindings.

Backing really fast within the app and using textChanged() in onResume() with a call to getPresenter on Subscribe got us a null exception which indicated that Presenters dependencies were not injected.