TakefiveInteractive/Ledger-Android

Realm will crash when device orientation changes

Opened this issue · 5 comments

This bug can be reproduced (but not related to changes brought) in commit 114a172, by changing the device orientation while browsing MainActivity.

Caused by: java.lang.IllegalStateException: This Realm instance has already been closed, making it unusable.
   at io.realm.BaseRealm.checkIfValid(BaseRealm.java:344)
   at io.realm.Realm.where(Realm.java:880)
   at com.takefive.ledger.MainActivity.onCreate(MainActivity.java:110)
   at android.app.Activity.performCreate(Activity.java:5990)
   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2311)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2420) 
   at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3980) 
   at android.app.ActivityThread.access$1000(ActivityThread.java:154) 
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1327) 
   at android.os.Handler.dispatchMessage(Handler.java:102) 
   at android.os.Looper.loop(Looper.java:135) 
   at android.app.ActivityThread.main(ActivityThread.java:5294) 
   at java.lang.reflect.Method.invoke(Native Method) 
   at java.lang.reflect.Method.invoke(Method.java:372) 
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:910) 
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:705) 
   at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:114)
``` 

Need to better handle view lifecycle events.

I am thinking about using Provider rather than directly injecting (in the
latter way the pointer will not change even if activity has been stopped
and restarted.)

On Sunday, February 14, 2016, lafickens notifications@github.com wrote:

Need to better handle view lifecycle events.


Reply to this email directly or view it on GitHub
#9 (comment)
.

I concur. Realm instances should always be retrieved from providers and closed in lifecycle events when appropriate.

I just looked it up and actually it was a re-creation rather than
re-starting, and that's why the code in onCreate is called again and
accessed Realm. BTW, when shall we close the Realm connection? Do we create
our Activity base class to do that?

Reference:
http://developer.android.com/training/basics/activity-lifecycle/recreating.html#SaveState

On Sunday, February 14, 2016, lafickens notifications@github.com wrote:

I concur. Realm instances should always be retrieved from providers and
closed in lifecycle events when appropriate.


Reply to this email directly or view it on GitHub
#9 (comment)
.

If we were to make a base activity, then it only makes sense to use on single realm within a single activity (and casting to the base class type perhaps?) . That can be tricky to manage sometimes. I would suggest calling Realm.close() in onPause and somehow reopen realm in onResume. Currently I only have realm closed in onDestroy.