Android-cleanarchitecture: NPE in fragment inject

Created on 12 Mar 2016  Â·  4Comments  Â·  Source: android10/Android-CleanArchitecture

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.

Most helpful comment

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

All 4 comments

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.

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.

Could this be provided as PR here?

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

StreetFlaneur picture StreetFlaneur  Â·  3Comments

ZherebtsovAlexandr picture ZherebtsovAlexandr  Â·  3Comments

Joy-Whale picture Joy-Whale  Â·  5Comments

ashraffouad picture ashraffouad  Â·  5Comments

connexion2000 picture connexion2000  Â·  3Comments