pvllnspk
pvllnspk

Reputation: 5767

Android: return to the Application Home Activity

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?

Upvotes: 1

Views: 3219

Answers (2)

swiftBoy
swiftBoy

Reputation: 35783

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?

Upvotes: 1

Leandros
Leandros

Reputation: 16825

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();

Upvotes: 2

Related Questions