Returning to main menu in android by a button click
Question
In android, my app provides a button that the user can click to return them back to the screen that appears when the app is opened (onCreate).
How can I set that button to return the user to the main menu?
I have something like this in a switch statement (on click):
case R.id.return_main:
onCreate();
return;
Where return_main is the id of the button....I know that isn't right but I couldn't think of any other way.
Thanks!
Solution
Use an intent to re-launch your main activity:
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
OTHER TIPS
You should be able to just call finish(). If you are in an Activity that is a child of your main Activity, this will return you to that main Activity screen.
Intent intent = new Intent(this, MainActivity.class); startActivity(intent);
You have to kill the activity to return to the Main Menu, for example I called Activity1 from Menu and kill it:
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();
}
});