如何实现Android的动作条后退按钮?如何实现、按钮、动作、Android

2023-09-13 02:28:27 作者:人帅是非多

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

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>

http://developer.android.com/design/patterns/navigation.html#up-vs-back

 
精彩推荐