LoginSignup
24
12

More than 5 years have passed since last update.

Android ログイン後にログイン画面に戻れないようにしたい

Last updated at Posted at 2018-12-10

ログイン後にメイン画面に遷移するアプリを実装するとき、メイン画面から戻るボタンでログイン画面に戻れないようにしたい。

そんなときはIntentに以下のようなフラグを設定してActivityを起動する。

loginButton.setOnClickListener {
    val intent = Intent(this@LoginActivity, MainActivity::class.java)
            .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK)
    startActivity(intent)
}

フラグの意味

  • FLAG_ACTIVITY_NEW_TASK: 新しいタスクを作成し、起動するActivityをそのタスクのスタックに追加する
  • FLAG_ACTIVITY_CLEAR_TASK: Activityを起動する前に、既存のタスクを破棄する。このフラグはFLAG_ACTIVITY_NEW_TASKと組み合わせてのみ使用できる。

参考

24
12
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
24
12