yhishi
yhishi

Reputation: 33

The intent data remains, when I close the app by the back button and restart from the task

I developed a deep link app. The intent data remains, when I close the app by the back button and restart from the task After launching from deep link.

Manifest:

<application
    <activity
        android:name=".MainActivity"
        android:label="@string/appName"
        android:launchMode="singleTask">
        
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />

            <data
                android:host="news"
                android:scheme="example" />

        </intent-filter>
    </activity>

    <activity
        android:name=".NextActivity" />

</application>

MainActivity:

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        if (intent.action == Intent.ACTION_VIEW && Uri.parse(intent.data.toString()).host == "news") {
            // Transition NextActivity
        }
    }
}

In case of startup after closing with the back button, the code of "Transition Next Activity" is passed many times.

I tried this but did not work.

intent.data = null
setIntent(intent)

Upvotes: 2

Views: 1048

Answers (1)

David Wasser
David Wasser

Reputation: 95628

I've answered a similar question about "extras" in an Intent in a Notification. The problem is the same. When you launch an app for the first time (in your case via deep link), Android remembers the Intent that was used to launch the app. When your user returns to the app from the "recent tasks" list, Android launches the app using the same (remembered) Intent. To get around this you have 2 options:

  1. Add Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS in your deep-link. This will prevent the task from showing up in the list of recent tasks and therefore precent the deep-link Intent from being remembered.

  2. Detect Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY. When the user launches an app from the list of recent tasks, Android adds this flag to the Intent that is passed to your Activity's onCreate(). You can check for the existence of this flag, and if it is set, you can clear the data from the Intent so that your app does not perform the same actions as when launched by deep-link.

See my answer here for more info: https://stackoverflow.com/a/19820057/769265

Upvotes: 2

Related Questions