androidandroid-activityandroid-manifestandroid-task

How can I have only one process of the same app running?


I have an issue with the Android app I'm developing which makes the system run 2 processes of the same app when I press the "up" action bar button in one particular activity.

To be more specific, let's call that activity Z and its parent, as declared in the manifest, Y. If I start the activity Z from an activity X and press the up action bar button, the activity Y will be started and then I'll have 2 processes running: one with activity Y and another with Z.

I've already tried setting different launch flags for that activity but the issue persists.

This activity in question shares the same task affinity as 5 others in the app but it's the only one that seems to cause this behaviour.

Here is a snippet of my manifest with the activities in the same affinity as the one causing the issue and a screenshot proving that there are 2 processes of the app running.

Manifest

        <activity android:name=".feature.stations.X"
            android:taskAffinity="@string/affinity_petrol_stations"
            android:theme="@style/AppTheme.NoActionBar"/>

        <activity android:name=".feature.stations.Y"
            android:taskAffinity="@string/affinity_petrol_stations"
            android:theme="@style/AppTheme.NoActionBar">
            <meta-data android:name="android.support.PARENT_ACTIVITY"
                android:value=".feature.stations.X"/>
        </activity>

        <!-- THIS IS THE ACTIVITY THAT CAUSES THE ISSUE -->
        <activity android:name=".feature.stations.Z"
            android:taskAffinity="@string/affinity_petrol_stations"
            android:theme="@style/AppTheme.NoActionBar">
            <meta-data android:name="android.support.PARENT_ACTIVITY"
                android:value=".feature.stations.Y"/>
        </activity>

Screenshot 2 processes running

The process at the top shows activity Z and the one at the bottom shows Y.


Solution

  • I managed to solve my problem by removing the meta-data tag from that activity and then implementing manually the up button behaviour like the example below:

    override fun onOptionsItemSelected(item: MenuItem?): Boolean {
        when (item?.itemId) {
            ...
            android.R.id.home -> finish()
        }
        return super.onOptionsItemSelected(item)
    }