Android的后退按钮不会返回到previous活动 [英] Android Back Button Doesn't Return to Previous Activity

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

问题描述

我有一个应用程序,它有两个活动:MainActivity和SettingsActivity。该MainActivity具有一个设置菜单项的菜单。当这个菜单项被点击时,它将启动与意图SettingsActivity。之后,活动开始时,我点击后退按钮在左上角并没有任何反应。我以为,因为我开始使用意向的活动,活动的堆栈将自动管理。我想回到MainActivity。我错在这种假设?

MainActivity.onMenuItemSelected

 公共布尔onMenuItemSelected(INT FEATUREID,菜单项项){
    INT ITEMID = item.getItemId();

    如果(ITEMID == R.id.settings){
        意向意图=新的意图(这一点,SettingsActivity.class);
        startActivity(意向);
    }

    返回true;
}
 

SettingsActivity

 公共类SettingsActivity扩展preferenceActivity {

    公共静态最后弦乐TEST =测试;

    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        加preferencesFromResource(R.xml preferences。);
    }
}
 

解决方案

在你的 SettingsActivity ,你需要重写 onOptionsItemSelected 启用对回去的动作栏的左上角的返回按钮。它本身并不知道,它需要做的点击。外壳里面 android.R.id.home ,你可以叫完成()。这将完成您目前的活动,你会回去 MainActivity 这开始​​了它。例如:

  @覆盖
    公共布尔onOptionsItemSelected(菜单项项){
        开关(item.getItemId()){
        案例android.R.id.home:
            this.finish();
            返回true;
        }
        返回super.onOptionsItemSelected(项目);
    }
 

只是为了保持完整性,使home键,您可以添加在你的的onCreate() SettingsActivity :

  getActionBar()setDisplayHomeAsUpEnabled(真)。
 

由于每<一个文档href="http://developer.android.com/reference/android/app/ActionBar.html#setDisplayHomeAsUpEnabled%28boolean%29"相对=nofollow> setDisplayHomeAsUpEnabled()

这是要表明,选择家庭会返回一个级别,而不是应用程序的顶级用户。

希望这有助于。

I have an app that has two activities: MainActivity and SettingsActivity. The MainActivity has a menu with a single Settings menu item. When this menu item is clicked, it launches the SettingsActivity with an intent. After the activity starts up, I click the back button in the top left corner and nothing happens. I assumed since I started the activity using an intent, the activity stack would be managed automatically. I want to return to the MainActivity. Am I wrong in this assumption?

MainActivity.onMenuItemSelected

public boolean onMenuItemSelected(int featureId, MenuItem item) {
    int itemID = item.getItemId();

    if(itemID == R.id.settings) {
        Intent intent = new Intent(this, SettingsActivity.class);
        startActivity(intent);
    }

    return true;
}

SettingsActivity

public class SettingsActivity extends PreferenceActivity {

    public static final String TEST = "test";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);
    }
}

解决方案

Inside your SettingsActivity you need to override onOptionsItemSelectedto enable the back button on top left corner of Action Bar for going back. It does not know by itself that what it needs to do on click. Inside the case android.R.id.home you can just call finish(). This will finish your current activity and you will go back to MainActivity which started it. Eg:

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            this.finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

Just for completeness, to enable the home button, you may add the following in your onCreate() of SettingsActivity:

getActionBar().setDisplayHomeAsUpEnabled(true);

As per docs of setDisplayHomeAsUpEnabled()

It is to show the user that selecting home will return one level up rather than to the top level of the app.

Hope this helps.

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

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