Issue
There is one Activity
- called MainActivity
. There is one xml
- called activity_main
. It contains a menu layout and game layout.
Menu layout - contains a "Start Match" Button
Game layout - contains a Listview
with 4 String
items (4 words...) and a "Done" Button
When the app launches, the menu layout is visible and game layout hidden. When the "Start Game" Button
is clicked, the menu layout is hidden and the game layout is shown. When the "Done" Button
is clicked the menu layout is shown and the game layout is hidden.
Problem: When I start a new game after clicking the "Done" Button
, the previous games data is loaded onto the current game:
The Listview
has 8 items instead of 4.
I assume it has to do with the onSaveInstanceState
: onDestroy()
is never called so when I click on "Start Match" a second time, it loads the state of the previous game.
Also, onResume()
gets called after clicking "Start Match" the second time, so onCreate()
and onStart()
never get called again - unless I exit the app and launch again.
I'm not sure how to solve this issue. I tried making the onSaveInstanceState
null but that did not help:
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
if(newMatch)
outState=null;
Log.d(TAG, "onSaveInstanceState(): onSaveInstanceState called");
}
Can someone please help?
Please tell me if I need to provide more code or info.
Solution
Why don't you write 2 classes instead of just one?
- One class would be for the menu, with just one layout
- The second one for the game itself
With these you ensure the values of the game are always loaded in the same way, without being affected by previous games.
Answered By - fapps
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.