1

I am trying to use the next code to return back to the My Application Home Activity from the stack of my application:

protected void goHome(boolean offlineMode) {
    final Intent intent = new Intent(this, MainActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(intent);
...
}

But in this case all other applications activities on the top will be closed too. Is it possible to close activities only of my application in this case?

TN888
  • 7,659
  • 9
  • 48
  • 84
pvllnspk
  • 5,667
  • 12
  • 59
  • 97

2 Answers2

2

This should do the work.

Intent i = new Intent(context, HomeActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
finish();
Leandros
  • 16,805
  • 9
  • 69
  • 108
1

You can also implement this from your AndroidManifest.xml file, just adding

android:noHistory="true" 

attribute in those <activity> you want (eg. HomeActivity)

see here is the sample code of manifest

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.rdc.helloWorld"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".HelloWorldActivity"
            android:label="@string/app_name" 
            android:noHistory="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

so when you click back button on Home Activity screen, it will clear history of this screen only

You may like read about noHistory tag details,

How does android:noHistory=“true” work?

Community
  • 1
  • 1
swiftBoy
  • 35,607
  • 26
  • 136
  • 135