【问题标题】:How to return to previous activity through action bar?如何通过操作栏返回上一个活动?
【发布时间】:2014-05-17 13:30:15
【问题描述】:

我有一个带有动作栏的活动,我想返回上一个,我该如何使用动作栏?

【问题讨论】:

标签: android android-layout android-fragments android-actionbar


【解决方案1】:

添加getActionBar().setDisplayHomeAsUpEnabled(true);

并覆盖onOptionsItemSelected(),如下所示:

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

还要在清单文件中指定父活动:

例如:

<application ... >
...
    <!-- The main/home activity (it has no parent activity) -->
    <activity
        android:name="com.example.myfirstapp.MainActivity" ...>
        ...
    </activity>
    <!-- A child of the main activity -->
    <activity
        android:name="com.example.myfirstapp.DisplayMessageActivity"
        android:label="@string/title_activity_display_message"
        android:parentActivityName="com.example.myfirstapp.MainActivity" >
        <!-- Parent activity meta-data to support 4.0 and lower -->
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.myfirstapp.MainActivity" />
    </activity>
</application>

【讨论】:

  • OP想去previuos活动,而不是home活​​动,所以他/她不需要startActivity(intent);,只需输入finish方法
  • 啊!!对于那个很抱歉。我已经改变了答案。
猜你喜欢
  • 2019-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-26
  • 1970-01-01
相关资源
最近更新 更多