android - Save Activity/Fragments state custom class -
i know if there method save custom class while rotating in android app. want save instanced object called of matrixgame class...someone can me?
thanks
since fragment lifecycle independent - when you're using fragment can set doesn't destroyed upon configuration changes.
as noticed, activity class gets destroyed , re-created when rotate app (or apply other configuration changes), if want persist activity state can use sqlite , save whatever need in onpause()
method. in oncreate()
method check db last known state.
if want avoid saving state "forever" (meaning, user turns off app , tomorrow when turns on - want start fresh , not load last known state) can add timestamp , set threshold which, if passed, data considered stale , gets disregarded.
two comments:
- as @saprvade wrote, fragments gets destroyed default, in order prevent should call:
setretaininstance(true)
- in case want implement @saprvade's other suggestion (have activity ignore rotation changes) can see following explanation of how implement: https://stackoverflow.com/a/456918/1057429
Comments
Post a Comment