문제

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!

도움이 되었습니까?

해결책

Use an intent to re-launch your main activity:

Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);

다른 팁

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();                           
            }
        });
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top