pressing后退按钮没回去previous活动的Andr​​oid [英] Pressing Back Button did not go back to previous activity Android

查看:87
本文介绍了pressing后退按钮没回去previous活动的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读通过关于这个有很多今天的主题。然而,所有我见过的解决方案,没有对我一个解决方案。我是新的Andr​​oid开发。我的问题是,我在我的主页上的两个按钮 - 即琐事和帮助。该琐事onClicked()将引导用户到难度页,用户可以选择简单,中等,困难模式。我要的是,当用户pressed了Android早在看病难页按钮,它会返回到主菜单(即那里的琐事和帮助可以看出),因为正在发生的事情在我的情况下,是,它可以追溯到到Android菜单。我完成了我的活动,如下图所示,我尝试了不同的方法在我的两个按钮(帮助和琐事),但并没有给我任何的运气。

这是我的MainMenu。

 包com.kfc;

进口android.app.Activity;
进口android.content.Intent;
进口android.os.Bundle;
进口android.view.View;
进口android.view.Window;
进口android.widget.ImageButton;

公共类NewKFCActivity延伸活动{
    / **第一次创建活动时调用。 * /
    的ImageButton bPlay,bHelp;
    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        的setContentView(R.layout.main);

    bPlay =(的ImageButton)findViewById(R.id.playGame);
    bPlay.setOnClickListener(新View.OnClickListener(){
        @覆盖
        公共无效的onClick(视图v){
            // TODO自动生成方法存根
            意向意图=新的意图(v.getContext(),SelectDifficulty.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
            NewKFCActivity.this.startActivity(意向);

        }
    });

    bHelp =(的ImageButton)findViewById(R.id.Help);
    bHelp.setOnClickListener(新View.OnClickListener(){
        @覆盖
        公共无效的onClick(视图v){
            // TODO自动生成方法存根
            意向意图=新的意图(v.getContext(),HelpPageOne.class);
            startActivity(意向);
            完();
        }
    });


}
@覆盖
保护无效的onPause(){
    // TODO自动生成方法存根
    super.onPause();
    完();
}
}
 

解决方案

不要单击帮助菜单中完成你的活动。一旦你调用完成,该活动将被销毁,从活动的堆栈中删除(所以当你preSS回来了,它不再是previous活动)。

您应该只完成该活动时,它是真正完成(即用户已完成不管它是什么,该活动是应该让他们做)。你需要考虑的一系列活动,在您的应用程序堆栈。每次启动一个活动,它被放置在堆栈的顶部。如果你想后退按钮带你回到previous活动,不完成它(每次从栈中称光洁度它移除以来)。每次用户presses背面,当前活动从堆栈弹出和系统恢复它下面的活性。如果是从你在栈上的应用程序,当用户presses回来,您的应用程序完成后没有更多的活动,用户返回到主屏幕。

I was reading through a lot of topics today regarding this. However, all the solutions that I had seen, failed to be a solution for me. I am new in Android development. My problem is that I had two buttons in my main page - namely PlayGame and Help. The PlayGame onClicked() will direct the user to the "difficulty page" where the user can choose Easy, Medium or Hard mode. What I want is that when the user pressed the Android back button in the "difficulty page", it will go back to the main menu (i.e. where the PlayGame and Help can be seen), because what is happening in my case is that it goes back to the Android menu. I finished my activity as shown below and I tried different approach in my two buttons (Help and PlayGame) but did not give me any luck.

This is my mainmenu.

package com.kfc;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ImageButton;

public class NewKFCActivity extends Activity {
    /** Called when the activity is first created. */
    ImageButton bPlay, bHelp;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);

    bPlay = (ImageButton) findViewById(R.id.playGame);
    bPlay.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent intent = new Intent(v.getContext(), SelectDifficulty.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
            NewKFCActivity.this.startActivity(intent);

        }
    });

    bHelp = (ImageButton) findViewById(R.id.Help);
    bHelp.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent intent = new Intent(v.getContext(), HelpPageOne.class);
            startActivity(intent);
            finish();
        }
    });


}
@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    finish();
}
}

解决方案

Don't finish your activity on click of the help menu. Once you call finish, the activity will be destroyed and removed from the activity stack (so when you press back, it's no longer the previous activity).

You should only finish the activity when it is truly done (i.e. the user has completed whatever it is that the activity was supposed to let them do). You need to think of the set of activities in your application as a stack. Each time you start an activity, it is placed at the top of the stack. If you want the back button to take you back to the previous activity, don't finish it (since each time you call finish it removes it from the stack). Every time the user presses back, the current activity is "popped" from the stack and the system resumes the activity below it. If there are no more activities from your app on the stack when the user presses back, your app is finished and the user goes back to the home screen.

这篇关于pressing后退按钮没回去previous活动的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆