在android中,我的应用程序提供了一个按钮,用户可以单击该按钮将它们返回到打开应用程序时显示的屏幕(onCreate)。在Android中通过按钮单击返回到主菜单
如何设置该按钮以将用户返回到主菜单?
我有一个switch语句像这样(在点击):
case R.id.return_main:
onCreate();
return;
哪里return_main是按钮的ID ....我知道这是不对的,但我想不出任何其他方式。
谢谢!
在android中,我的应用程序提供了一个按钮,用户可以单击该按钮将它们返回到打开应用程序时显示的屏幕(onCreate)。在Android中通过按钮单击返回到主菜单
如何设置该按钮以将用户返回到主菜单?
我有一个switch语句像这样(在点击):
case R.id.return_main:
onCreate();
return;
哪里return_main是按钮的ID ....我知道这是不对的,但我想不出任何其他方式。
谢谢!
使用的意图,重新启动您的主要活动:
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
你应该能够只是调用finish()。如果您处于主活动的子活动中,则会返回到主活动屏幕。
Intent intent = new Intent(this,MainActivity.class); startActivity(intent);
你必须杀死活动返回到主菜单,比如我从菜单中调用活动1,杀了它:
ActMenu.java
startActivity(new Intent(ActMenu.this,Activity1.class));
Activity1.java
Button btnForm = (Button) this.findViewById(R.id.btnForm);
btnForm.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
finish();
}
});
我不得不补充:确保你在意图上设置了CLEAR_TOP,或者它只会在当前活动的* top *上启动你的家庭活动(也就是说,没有clear-top,从B到MainActivity,然后打“回”会仍然把你带回到活动B)。 “主页”按钮的大多数用途意味着点击“返回”将退出应用程序,这基本上是clear_top所做的。 http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP – 2010-11-22 19:35:59