无法返回上一页

时间:2020-09-26 15:20:38

标签: android settings

我创建了一个设置活动。当用户单击设置时,它会从维护活动转移到设置活动,但是当我单击操作栏顶部的后退按钮时,它不会转到上一页,它说应用程序不断停止。如果我单击“后退电话”按钮,它将起作用。

在此附上我的编码,请更正它。我该怎么办?

a

2 个答案:

答案 0 :(得分:0)

在活动中最好使用finish(),因为它会自动返回到上一个活动。

答案 1 :(得分:0)

首先,将其添加到XML文件中的收费栏

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary" >
        <ImageView
            android:id="@+id/home"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_back"/>
    </androidx.appcompat.widget.Toolbar>

  @Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            Intent intent = new Intent(this, HomeActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}