开发者

How do I set the layout to the previous layout when the back button is pressed?

I have the following code to handle button presses in my app:

if (keycode == KeyEvent.KEYCODE_BACK && CURRENT_VIEW == MAIN_MENU_ACTIVE) {
        Window window = this.getWindow();

        window.setContentView(R.layout.main);
        CURRENT_VIEW = MAIN_MENU_ACTIVE;

        return true;

    }

When it's run though, android just jumps straight to the return statement, instead of changing the layout. Is there anything that I am doing wrong here? This is for a game, so essentially I want the user to be able to navigate from the game creati开发者_开发百科on screen back to the main menu by hitting the back button.


just use setContentView(R.layout.main);. Don't start using a Window, as i'm fairly sure you don't need to.


You shouldn't call setContentView multiple times. Why not simply use finish(), which would return the user to the previous activity?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜