I did mine the wrong way - by having 1 activity that flips the visible
view and knows its state.
For my next game I am having a Game Menu activity and then the Game
activity. This will keep things much more clean and is easier to work
with state management. I used the single instance model which I
believe is good for games (depending on the type of game) and that has
some implications for lifecycle management.
On Nov 3, 4:46 pm, AaronBC <[EMAIL PROTECTED]> wrote:
> Okay, here's how I implemented the resume game function:
>
> In the onPause method I save the game state (Object) to a file:
>
> private void SaveGame()
> {
> FileOutputStream fileOutputStream = null;
> ObjectOutputStream objOutStream = null;
> try
> {
> fileOutputStream = openFileOutput(PlayActivity.SAVE_FILE_NAME,
> Context.MODE_PRIVATE);
> objOutStream = new ObjectOutputStream(fileOutputStream);
> objOutStream.writeObject((Deck)this.gameDeck);
> objOutStream.close();
> Log.v(this.getLocalClassName(), "writing file");
> }
> catch (Exception FileNotFoundException)
> {
> Log.v(this.getLocalClassName(), "File not found exception!:" +
> PlayActivity.SAVE_FILE_NAME);
> }
>
> }
>
> In the onCreate() method I restore the game state from the file:
>
> try
> {
> fileInputStream = openFileInput(PlayActivity.SAVE_FILE_NAME);//
> this.openFileInput(PlayActivity.SAVE_FILE_NAME);
> objInStream = new ObjectInputStream(fileInputStream);
> this.gameDeck = (Deck)objInStream.readObject();
> objInStream.close();
> displayCard = gameDeck.GetCurrentCard();}
>
> catch (Exception FileNotFoundException)
> {
> Log.v(this.getLocalClassName(), "File not found exception!:" +
> PlayActivity.SAVE_FILE_NAME);
> this.gameDeck = new Deck();
> displayCard = gameDeck.GetNextCard();
>
> }
>
> I am using an ObjectOutput/ObjectInput stream to handle saving and
> loading the card deck. A 1.8Kb file is created when I save the
> object. This seems like a resource heavy implementation. Is there a
> better way of doing this for an embedded application?
>
> Also, in order for the main menu to know whether a game has already
> been started, it just checks whether the file exists or not:
> protected void onStart()
> {
> String[] fileList,gameMenu;
> super.onStart();
> fileList = this.fileList();
> gameMenu = mStrings;
> for (String e: fileList)
> {
> if (e.equals(PlayActivity.SAVE_FILE_NAME))
> {
> gameMenu = mStringsResume;
> }
> }
> // Prepare the ListView
> final ArrayAdapter<String> adapter = new
> ArrayAdapter<String>(this,
> android.R.layout.simple_list_item_1, gameMenu);
>
> setListAdapter(adapter);
>
> }
>
> Another question: Rather than using fileList() to determine if the
> file exists, could I just try to open the file and catch the exception
> if it doesn't exist, or is this more resource heavy as well?
>
> Thx for any input,
> Aaron
>
> On Oct 29, 12:08 pm, AaronBC <[EMAIL PROTECTED]> wrote:
>
> > I'm writing a simplegameso I can learn more about android
> > development. I need a little help designing themenusystem. When
> > you start the application you will see a simplemenu:
>
> > Start NewGame
> > ResumeGame(Dynamically shown after agamehas been started)
> > Scoreboard
> > Instructions
> > About
>
> > When you click on "Start NewGame" it starts a new "Play" activity
> > (This works fine)
>
> > The "ResumeGame" option should only be visible when agamehas
> > already been started and the user returns to the mainmenu. How do I
> > determine whether the "Play" activity has been started or not in order
> > to display thismenuitem? Also, how do I handle the back button
> > press when in the "Play" activity so that thegamestate is paused and
> > not lost when returning to the mainmenu?
>
> > Example:
> > Thegameapplication is started and the user sees the following screen
>
> > NewGame
> > Scoreboard
> > Instructions
> > About
>
> > The user then taps NewGameand starts to play. In the middle of thegamethe
> > user hits the back button (or receives a phone call, starts
> > another app, etc.) and sees the following screen when returning to
> > thegameapp:
>
> > NewGame
> > ResumeGame
> > Scoreboard
> > Instructions
> > About
>
> > The user either taps "NewGame" and starts a newgameor taps "ResumeGame" and
> > continues where he left off.
>
> > How should I approach this using multiple Activities?
>
> > Thx,
> > Aaron
>
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---