LoginSignup
91
85

More than 5 years have passed since last update.

最初のActivityに戻る

Last updated at Posted at 2012-12-26

A→B→C→Dなどと複数のアクティビティを遷移した後、最初のAに戻る実装。

DActivity.java
Intent intent = new Intent(DActivity.this, AActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
  • FLAG_ACTIVITY_CLEAR_TOPは、遷移先のアクティビティが既に動いていればそのアクティビティより上にある(この場合はB, C, D)アクティビティを消す、という挙動を設定する。これによって、A→B→C→D→Aと遷移した後にbackボタンを押してもDに戻ることはなくなる。
  • FLAG_ACTIVITY_SINGLE_TOPは、既に動いているアクティビティに遷移するとそのアクティビティを閉じてもう一度作りなおすデフォルトの挙動(multiple mode)から、作りなおさずに再利用する挙動に変更する。これによって、D→Aへの遷移のときのアニメーションが戻る動きになる。

参考

91
85
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
91
85