i'm computer hobbyist working on small trivia game android platform.
in game, move player between multiple activity/view pages (entry, question page, answer page, ultimate score page). i've created question object contains data related single trivia question (question, 4 answers, pointer correct answer, related comments computer makes when responding player's guess), , i've populated arraylist of question objects data dom parsed xml file.
what acceptable way of passing data , forth between activities game progresses? how can keep these data objects alive user moves through game?
i understand global variables highly discouraged. have learned singleton design pattern poses many of problems said associated global variables. i'm stumped when comes finding alternatives approaches.
i understand basics of mvc approach. activity *.java files controller files, , they're linked layouts (views) i've created xml. have 2 "model" objects need maintained , modified game progresses: (1) question-bank arraylist mentioned above , (2) sort of "playerprogress" object contains data related player's progress in game.
these data objects first instantiated @ beginning of game, don't know how keep them alive user moves between activities. know can pass information between activities extras, don't seem intended purpose. if extras worked player progress properties, don't think can use them pass arraylist of 25-50 question objects between activities.
i have looked serialization , parcelable, seems weird (and possibly inefficient) decompose , recompose data model objects every time user moves , forth between different activities/views. if 1 of these acceptable/common way accomplish this, can happily plunge forward, wanted check others first.
i keep butting head against in different programming languages, , suspect there bigger part of picture i'm failing understand. i've read through descriptions of objects , application life cycles in many different resources, still haven't managed figure out solution basic question.
i'm asking question ("what acceptable way of passing data objects , forth between activities game progresses") in context of silly trivia game, i'm concerned getting handle on bigger picture. if folks don't have time spell out seemingly basic, perhaps can point me descriptions in other books found useful? (i've got few slots open in safari bookshelf, , can track down copies of technical publication.)
thanks.
i haven't got enough votes answer directly in 323go's post static variables... implementation is singleton. wanted point out because stated in question also learned singleton design pattern poses many of problems said associated global variables.
i admit extras quite weird first time if you're used call function or program parameters inline, seems it's usual way in android. don't see what's problem using extras, since it's transparent if model objects not complex. mean need implement "serializable" interface , it's done. there data types not working "by itself" serializable (as lists) , you'll need implement parcelable, can declare arrays insted. seems parcelable "far more efficient", if wanna better performing you'll need implement parcelable anyway said here: android: difference between parcelable , serializable?
i don't think shared preferences intended used "passing arguments tool" since wouldn't efficient has write , read physical memory, use "hard job" in computer system; while exras sent through memory stack (disclaimer: didn't tested, it's logic tells me). use dannyroa's way (storing json objects) time-persisting issues user preferences, internet-retrieved hardly-updated-data, scorings, savegames....
and, finally... maybe can restructure code. opening new activity each question? maybe can use 1 activity , use fragments. then, can have global variable in activity , fragment inside can access there getactivity().
((myactivity)getactivity()).myglobalvariable();
Comments
Post a Comment