TakefiveInteractive / Ledger-Android

An Android Application "Ledger".
4 stars 0 forks source link

Realm will crash when device orientation changes #9

Open markzyu opened 8 years ago

markzyu commented 8 years ago

This bug can be reproduced (but not related to changes brought) in commit 114a1721a1605d3a115b36e95a18c3bfd30bc2a5, 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)
``` 
mar29th commented 8 years ago

Need to better handle view lifecycle events.

markzyu commented 8 years ago

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 https://github.com/TakefiveInteractive/Ledger-Android/issues/9#issuecomment-184032009 .

mar29th commented 8 years ago

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

markzyu commented 8 years ago

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 https://github.com/TakefiveInteractive/Ledger-Android/issues/9#issuecomment-184033048 .

mar29th commented 8 years ago

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.