恢复一个井字棋游戏在Android中关闭片段后, [英] Resuming a Tic-tac-toe game in Android after closing the fragment
问题描述
我通过创建一个测试项目学习Android开发。我的井字棋应用开始以按钮
新游戏,上面写着一个主菜单的活动。之后点击新游戏,用含有井字棋游戏(在GridLayout)将启动一个片段的活动。用户可以玩游戏,但是当他们回到主菜单,游戏状态不会被保存。
I'm learning Android development by creating a test project: Tic-tac-toe. My Tic-tac-toe app starts with a Main Menu activity with a Button
that says New Game. After clicking New Game, an activity with a fragment containing a Tic-Tac-Toe game (in a GridLayout) launches. The user can play the game, but when they go back to the Main Menu, the game state is not saved.
我要改变这一点,以便当用户返回到主菜单,就会看到一个新的按钮
所谓的继续(除新游戏按钮)。一旦用户点击继续,本场比赛之前,他们被打仍在继续。如果点击新游戏按钮,新游戏会像前推出。
I want to change this so that when the user goes back to the Main Menu, they will see a new Button
called "Continue" (in addition to the "New Game" Button). Once the user clicks "Continue", the game they were playing before continues. If the click the "New Game" button, a new game will be launched like before.
通过使用的onSaveInstanceState
方法和捆绑
savedInstanceState
,我能preserve的方向改变游戏状态(通过保存从底层井字游戏类中的数据)。下面我code说明我是如何做到这一点。我想这个时候再次做同样的事情 - 我看了一下,但我不太明白,以最好的方式开始。谁能告诉我正确的步骤?
By using the onSaveInstanceState
method and a Bundle
savedInstanceState
, I was able to preserve the game state on orientation changes (by saving the data from the underlying TicTacToe class). My code below shows how I did that. I would like to do something similar this time again - I have read about it, but I don't quite understand the best way to start. Can anyone show me the right steps?
请注意,我做这种编程方式(即设计的XML文件以外的布局)!
Note that I'm doing this programmatically (i.e. designing the layout outside the xml files)!
我的的MainMenu
类:
public class MainMenu extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_menu);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void startGame(View view) {
Intent intent = new Intent(this,MainGame.class);
startActivity(intent);
}
}
有相应的XML文件 activity_main_game
:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/new_game"
android:onClick="startGame"
/>
</RelativeLayout>
现在我的 MainGame.class
是这样的:
public class MainGame extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_game);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new BoardFragment()).commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_game, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public static class BoardFragment extends Fragment {
public TicTacToe t = new TicTacToe(); //A class I wrote that launches a simple TicTacToe game
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (savedInstanceState != null) {
t = new TicTacToe(savedInstanceState.getInt("TicTacToeData"),
);
}
//Graphics stuff here: variable rootView which contains the TicTacToe grid is defined
//and onClickListeners are added to the ImageViews in the GridLayout which makes corresponding
//changes to t.
return rootView;
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
savedInstanceState.putInt("TicTacToeData", t.getGameData());
}
}
}
感谢您的帮助。
Thanks for any help.
推荐答案
另一种解决方案是重新设计你的应用程序只有一个活动
(或 ActionBarActivity
)的子类和多个片段
子类,每个屏幕要显示。你可以保持一个参考两个片段在活动
。这会隐保留其中显示了游戏板的片段的状态。
Another solution is to redesign your app to have only a single Activity
(or ActionBarActivity
) subclass and multiple Fragment
subclasses for each screen you want to show. You can keep a reference to both fragments in your Activity
. This will implicitly retain the state of the fragment which displays the game board.
这篇关于恢复一个井字棋游戏在Android中关闭片段后,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!