后退按钮仅在一个 Activity 中停止工作

Back button stopped to work in only one Activity

后退按钮仅在一个 Activity 中停止工作。我有三个具有相同 parent 的活动。但是他们中的一个停下来向后退 (CurrentMovieActivity)。我不知道为什么。清单似乎是正确的。

可能的原因是什么?

我的清单:

<application
android:allowBackup="true"
android:icon="@mipmap/ic_movie_white_24dp"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_movie_white_24dp"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".activity.MovieActivity">
  <meta-data
    android:name="android.app.searchable"
    android:resource="@xml/searchable" />

  <intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <action android:name="android.intent.action.SEARCH" />

    <category android:name="android.intent.category.LAUNCHER" />
  </intent-filter>
</activity>
<activity
  android:label="@string/settings_title"
  android:name=".activity.SettingsActivity"
  android:parentActivityName=".activity.MovieActivity" />
<activity
  android:label="@string/current_title"
  android:name=".activity.CurrentMovieActivity"
  android:parentActivityName=".activity.MovieActivity" />
<activity
  android:label="@string/favourites"
  android:name=".activity.FavouritesActivity"
  android:parentActivityName=".activity.MovieActivity" />

<provider
  android:authorities="com.globallogic.v_holodynskyi.imdbclient"
  android:exported="false"
  android:name=".database.MovieProvider" />

我还尝试更改启动模式并覆盖 onBackPress。看起来像这样,但我看不到任何日志:

@Override
public void onBackPressed() {
  Log.i(LOG_TAG, "back pressed1");
  super.onBackPressed();
  Log.i(LOG_TAG, "back pressed2");
}

我的"broken"activity:

public class CurrentMovieActivity extends AppCompatActivity {
  private static final String LOG_TAG = CurrentMovieActivity.class.getSimpleName();

  private ImageView mPoster;
  private TextView mMovieTitle;
  private TextView mMovieDescription;
  private FloatingActionButton mFloatingButton;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_current_movie);

    initializeVariables();
  }

  @Override
  public void onBackPressed() {
    Log.i(LOG_TAG, "back pressed1");
    super.onBackPressed();
    Log.i(LOG_TAG, "back pressed2");
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater menuInflater = getMenuInflater();
    menuInflater.inflate(R.menu.current_menu, menu);
    return true;
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case R.id.action_favourite:
        Intent openFavourite = new Intent(this, FavouritesActivity.class);
        startActivity(openFavourite);
        break;
      default:
        break;
    }
    return true;
  }

  private void initializeVariables() {
    Intent intent = getIntent();

    final String title = intent.getExtras().getString(Intent.EXTRA_TEXT + TITLE);
    final String overview = intent.getExtras().getString(Intent.EXTRA_TEXT + DESCRIPTION);
    final String posterLocation = intent.getExtras().getString(Intent.EXTRA_TEXT + LOCATION);

    mPoster = findViewById(R.id.iv_movie_poster_current);
    downloadImage(posterLocation, mPoster);

    mMovieTitle = findViewById(R.id.tv_movie_title);
    mMovieTitle.setText(title);

    mMovieDescription = findViewById(R.id.tv_movie_description_current);
    mMovieDescription.setText(overview);

    mFloatingButton = findViewById(R.id.fb_add_to_favourites);
    mFloatingButton.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        saveMovie(CurrentMovieActivity.this, title, overview, posterLocation, CONTENT_URI);
      }
    });
  }
}

检查覆盖 onBackPress() 代码,因为您已经删除了其中的代码。在 onBackPress(),.

中添加 finish()

这可能是因为使用了android:parentActivityName。当您使用该属性时,它会使您的代码在 onOptionsItemSelected 方法中检查 android.R.id.home

您需要检查 android.R.id.home 并且 不要在开关中使用默认 大小写,如下所示:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
  switch (item.getItemId()) {
    ...
    case android.R.id.home:
      finish(); // call finish and don't use default case.
      break;
  }
  return true;
} 

这里是相关文档:Providing Up Navigation