如何实现Android的动作条后退按钮? [英] How to implement the Android ActionBar back button?

查看:133
本文介绍了如何实现Android的动作条后退按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表视图的活动。当用户单击该项目,该项目浏览器打开:

I have an activity with a listview. When the user click the item, the item "viewer" opens:

List1.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {

        Intent nextScreen = new Intent(context,ServicesViewActivity.class);
        String[] Service = (String[])List1.getItemAtPosition(arg2);

        //Sending data to another Activity
        nextScreen.putExtra("data", datainfo);
        startActivityForResult(nextScreen,0);
        overridePendingTransition(R.anim.right_enter, R.anim.left_exit);
    }
});

这工作得很好,但在动作条后退箭头旁边的应用程序图标不会被激活。我失去了一些东西?

This works fine, but on the actionbar the back arrow next to the app icon doesn't get activated. Am I missing something?

推荐答案

Selvin已经发布了正确的答案,在这里仅仅是pretty的code中的解决方案; - )

Selvin already posted the right answer, here is only the solution in pretty code ;-)

public class ServicesViewActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // etc...
        getActionBar().setDisplayHomeAsUpEnabled(true);
    }

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

功能 NavUtils.navigateUpFromSameTask(本)需要你定义在AndroidManifest.xml父活动文件

The function NavUtils.navigateUpFromSameTask(this) requires you to define the parent activity in the AndroidManifest.xml file

    <activity android:name="com.example.ServicesViewActivity" >
            <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.ParentActivity" />
    </activity>

<一个href="http://developer.android.com/design/patterns/navigation.html#up-vs-back">http://developer.android.com/design/patterns/navigation.html#up-vs-back

这篇关于如何实现Android的动作条后退按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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