I have a pretty extensive adventure game running on Android using flixel-gdx, and I would like to save the current game state when android switches to another application.
I know you can override the onFocus() and onFocusLost() methods on the FlxGame object, but I can't see a way of accessing the current game state from those methods. The only way I can see is to create a static object that holds a reference to the game state values, and refactor my entire game to reference the static object when it needs to manipulate those values.
Can anyone think of an alternative?
FlxG.getState();
returns you the current state that is running.
Related
I am new to android development (and also a java newbie) and I noticed that when I switch between views I noticed that any variables declared as static in the view class retains its value but the rest are gone. So I have the following questions if some one is kind enough to answer --
When to use static for variables if any?
If I want to retain the state of say my game between these switches (say to see the score or something), what is the way to do it? Is it by using static variables in the class to store everything? If so how do I reset the variables for a new game?
When to use static for variables if any?
Static variables should be use when multiple activities needs access on it. In other words, if you think your variable has global usage, then setting it as public static may help.
If I want to retain the state of say my game between these switches (say to see the score or something), what is the way to do it? Is it by using static variables in the class to store everything? If so how do I reset the variables for a new game?
You need to override onSaveInstanceState and onRestoreInstanceState to save and restore the values of your variables. A tutorial on how to use it can be found here: http://android-er.blogspot.com/2011/09/onsaveinstancestate-and.html .
I need to pass a List of my objects between activities. I do not want to use parcelable or serialize the data each time. I also do not want to save it in a local file or database. That probably leaves me with using static objects.
Lets say I to use ListA between activities Activity1 to Activity2. I ca
Approach1: Create a static ListA in one of those activities and do all my stuff of that static ListA.
Approach2: Create a static list in another class which I use just for storing this List and doing all my stuff on this list. But this means that this stays as long as my process is running and I have to manually set it to null when I do not need it.
Approach3. I am extending the above class to implement it using a static HashMap.
I have two methods one to store the list in a static HashMap using a unique key and another method to retrieve the list and remove it each time data is retrieved so that the List is no longer present in the static HashMap. So we essentially have to pass only the random key generated to store data between activities which I can pass as an extra using Intents.
Will there be any issues when I use any of the above approaches and which will be the best approach.
I'd consider creating an Application object and using it like a singleton to access your data. I've described the approach here: http://chrisrisner.com/31-Days-of-Android--Day-7%E2%80%93Sharing-Data-Between-Activities. Some people don't seem to like using the Application object in this manner but it makes more sense to me than putting a static object on an Activity.
Uggg statics! Man I wish all developers understood global variables are bad and how they make your program more brittle and your life hell. We only been talking about how bad they are for 30+ years, but unfortunately no one figures this out until they've utterly hung themselves on them.
First I'll say serializing your data is fast. There are great tools out there that will serialize your objects quickly that you can use I prefer http://flexjson.sourceforge.net for this.
So if you are just outright opposed to this you can pass this object through the Application by subclassing it, declaring your implementation in your Android Manifest, and each activity has access to the Application instance:
public class MyActivity extends Activity {
public void onCreate( Bundle bundle ) {
MyApplication application = (MyApplication)getApplication();
Object anInstanceFromAnotherActivity = application.getSomeInput();
}
}
The downside to this is when your application is reclaimed if the user returns to your application the memory is gone, and you can't get that input you might need of your screen. Android framework is trying to make you serializing things in the bundles because if it decides to destroy your application you can always rebuild yourself from the bundle. Now there are short cuts you can take like redirecting people to start over if the Application has been reclaimed, but those depend upon your program and what its doing if they make sense.
That's where using serialization wins out over all other forms of persistence (parcelables, files, databases) because it can be done in one line of code.
When adding an object to a Bundle in order to save it in onSaveInstance does it save the state of all the static variables or will then be reset when I reload using onRestoreInstance?
As an extension of the question, is it possible to save a non-instantiated class. In other words just the statics?
I am using it for a card game and since I will only ever have one deck of cards it seems silly to have to instantiate it when I can use all statics.
From an OO perspective, it would make more sense to model your deck of cards on the Singleton pattern rather than making it static. Then you can save that object in the Bundle and restore it without having to worry about the static issue. If you care about such things, this method also makes for 'better' object oriented programming.
I've got a small android application that implements the Observer pattern.
I want my "subject" (or the thing being observed) to change its state, such as changing its String from hello to hello world.
How can I do this via the emulator?
I need my (observing) android application to still run in the foreground so it can display whatever is being observed, but I need to find some way to alter the state of the observed object, is it possible to do this via the command line? How else can I "poke" the observed object to change its state.
One dirty-hack method I can think of is to set the observed object up in a loop, so every 10 seconds it changes its state, but that is not good as then I won't have control over it.
Correct answer: write a test case, and have the test case "poke" the observed object.
Not-quite-as-correct answer: add stuff to your activity to "poke" the observed object.
I am new to Android development.
I want to know how you all manage objects.
For eg we make object as A a = new A(); and then manage that object.
But, here, we call A.class;
My concern is that i dont want to call onCreate(),nor do i want to push UI screen.
I just want to make 1 static object for 1 screen;and want to manage it throughout my application
that is, instead of calling A.class; can i call A a = new A(),and manage that object without pushing,and whenever i need i push that screen. Is there someway ?
Thanks...
I just want to make 1 static object for 1 screen;and want to manage it throughout my application that is
That somehow describe what an Activity is for. Your complete question suggest that you have no idea how Android works and why it is meant to be. You should start at least with the fundamentals and than work through tutorials to get a feeling. Fundamentals can be found here: http://developer.android.com/guide/topics/fundamentals.html