Android: open activity without save into the stack

前端 未结 10 1872
暗喜
暗喜 2020-11-27 04:19

I have 2 activities: Main and List.

From Main you can open List; from List you can open Main.

I\'d like it so that every opening of List does not

相关标签:
10条回答
  • 2020-11-27 04:58

    It seems, if you call finish() on your activity right after you have opened another, the one that is finished is removed from the stack.

    for example:

    Intent intent = new Intent(this, NextActivity.class);
    startActivity(intent);
    finish();
    
    0 讨论(0)
  • 2020-11-27 05:00

    Just wanted to add a way to do this in Kotlin:

    val i = Intent(this, LogInActivity::class.java)
    startActivity(i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK))
    
    0 讨论(0)
  • 2020-11-27 05:01

    add the flag FLAG_ACTIVITY_NEW_TASK to your Intent

    http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_NEW_TASK

    0 讨论(0)
  • 2020-11-27 05:02

    In the manifest file add:

    android:noHistory="true" 
    

    to the activity that you don't want to keep on the stack.

    0 讨论(0)
  • 2020-11-27 05:08

    Try FLAG_ACTIVITY_CLEAR_TOP if the activity is already running:

    Intent intent = new Intent(this, MyActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(intent);
    
    0 讨论(0)
  • 2020-11-27 05:19

    Late answer, but adds some depth to other answers. It all comes down to what do you want to happen with other activities started from that activity

    Option 1 - Just this one activity should not have history of calling activity

    Then just do:

    Intent i = new Intent(...);
    i.addFlag(Intent.FLAG_ACTIVITY_NO_HISTORY);
    startActivity(i);
    

    Option 2 - All activities started from that specific activity should not have history

    Then add in manifest of the calling activity:

    android:noHistory="true" 
    

    But if you do want to have history in new activity, then you have to manually remove the flag:

    Intent i = new Intent(...);
    i.removeFlag(Intent.FLAG_ACTIVITY_NO_HISTORY);
    startActivity(i);
    

    Hope that clears up other answers :)

    0 讨论(0)
提交回复
热议问题