Android remove Activity from back stack

后端 未结 11 2038
予麋鹿
予麋鹿 2020-12-07 17:23

Okay so I\'m kind of stumped on what to do with this. So I have the MainActivity, and from there an Activity can be launched to DegreePlanActivity, and from there another Ac

相关标签:
11条回答
  • 2020-12-07 17:50

    To remove activity from back stack inside manifest add android:noHistory="true" to your activity inside the manifest file.

    See sample below.

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.example.activity"
          android:versionCode="1"
          android:versionName="1.0">
     <application android:name="MyApp" android:label="My Application">
        <activity android:name=".LoginActivity" 
          android:noHistory="true"> //add this line to your activity inside manifest
         <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
          </intent-filter>
       </activity>
     </application>
    </manifest>
    
    0 讨论(0)
  • 2020-12-07 17:54

    It seems, that you will get the desired behavior if you do not specify any flags at all. The back button would just do the right thing. To get an activity closed from within your code use the finish() method it has the same effect as the user pressing the back button. So you will automatically arrive at DegreePlan when you finish the EditDegreePlan, no need to call any Intents either.

    0 讨论(0)
  • 2020-12-07 17:55

    Here is your flow:

    MainActivity --> DegreePlanActivty --> EditDegreePlan--> DegreePlan --> MainActivty

    Override these method inside your "DegreePlan"

    public void onBackPressed() {
       super.onBackPressed();
       Intent goToMainActivity = new Intent(getApplicationContext(), MainActivity.class);
       goToMainActivity.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // Will clear out your activity history stack till now
       startActivity(goToMainActivity);
    }
    
    0 讨论(0)
  • 2020-12-07 17:56

    use this to clear the stack :

     menuIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_TOP);
    
    0 讨论(0)
  • 2020-12-07 17:58

    You can call finish before you start your new activity. This will remove the current activity from the stack, so when you press back button from the next activity, the first activity will not be called from the stack history.

    Intent i = new Intent(MainActivity.this, NextActivity.class);
    finish();  
    startActivity(i);
    
    0 讨论(0)
  • 2020-12-07 17:59
    This code should help you out: It is in Kotlin
    private fun verifyIfUserIsLoggedIn(){
            val uid = FirebaseAuth.getInstance().uid
            if(uid== null){
                val intent = Intent(this, MainActivity::class.java)
                intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TASK.or(Intent.FLAG_ACTIVITY_NEW_TASK)
                startActivity(intent)
            }
        }
    
    0 讨论(0)
提交回复
热议问题