android10 / Android-CleanArchitecture

This is a sample app that is part of a series of blog posts I have written about how to architect an android application using Uncle Bob's clean architecture approach.
Apache License 2.0
15.49k stars 3.33k forks source link

NPE in fragment inject #124

Open Cooki1e opened 8 years ago

Cooki1e commented 8 years ago

The problem is when the application stays in the backgound for a while and a recently displayed activity is recycled by Android. Bringing back the application causes NPE as the activity's component is called by the fragment before it is initialized. In my case it often happens after pushing on notification which starts new activity. This issue is very hard to reproduce. By the way this issue occurs only on my device with android 5.

FizzyMobile commented 8 years ago

https://github.com/android10/Android-CleanArchitecture/commit/3e2e3822796d6216a6b221a9ddbff58ab8134faa#commitcomment-16642857

To reproduce: switch ON Don't keep activities under Apps in Settings/Developer options. This will destory every activity as soon as the user leaves it.

Cooki1e commented 8 years ago

Well, I suppose i fixed this issue. The reason is that the code which create fragment component in activity doesn't executed. Most of the time it works well, but when activity goes to background and is called after some time, FragmentController tries to restore fragment (in onCreate() callback in FragmentActivity.class), that's why dependencies's injections in fragment happen before fragment component initialization in activity. That's why NPE occures. So to fix this behaviour i put fragment component initialization before super.onCreate() in my activity and the issue is gone.

Rainer-Lang commented 8 years ago

Could this be provided as PR here?

FizzyMobile commented 8 years ago

You may find this interesting: Efficient and bug-free fragment injection in Android MVP applications on medium.com