我有一个应用程序,它有活动(称为 MyFirstActivity),onCreate 是这样的:
MyView view;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game_sound_space);
RelativeLayout layout = (RelativeLayout) findViewById(R.id.myMain);
view = new MyView(this, getPreferences(Context.MODE_PRIVATE), layout);
layout.addView(view, 0); // add view to the layout
}
视图是这样的(这是构造函数):
Context context;//I need to do context global for using in other methods
public MyView(Context context,SharedPreferences sharedPreferences,
RelativeLayout parentLayout) {
super(context);
this.context = context;
}
显然在 onResume 方法的活动中我启动了视图:
@Override
public void onResume(){
super.onResume();
view.loadGame();
}
loadGame() 在我看来,它工作得很好,但在我看来,我希望重新开始整个游戏,我使用过:
Intent intent = new Intent(context.getApplicationContext(),MyFirstActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
但我怀疑这不是重新启动的正确方法(并且开始出错)也使用:
Intent intent = getIntent();
finish();
startActivity(intent);
这是不可能的,因为我在一个视图中并且 context.finish() 不起作用(不存在)
那么如何重新启动 firstActivity (MyFirstActivity) 以重新启动视图和游戏?需要帮助,谢谢。