Search code examples
androidandroid-activityandroid-manifestandroid-permissions

How to work with permissions in single activity design using navigation component?


My previous app has many activities and some of them are declared as exported true, so that other apps can call them through intent filter. Some of them also has specific permission which also sets in my activities.

Now, i am developing a app using single activity. So, i am curious to know how can i handle this permission issue in single activity design, as i have only one activity.

Previously, in multiple activities i can easily set permission in specific activity. How can i handle this here? I am a newbie. I searched a lot but did not find any clue.

Edit:

    Multiple activities declaration:

    <permission
      android:name="com.example.permission"
      android:protectionLevel="signatureOrSystem"
      tools:ignore="SignatureOrSystemPermissions" 
    />

    // Activity 1: With permission, exported = true
    <activity
        android:name=".place"
        android:configChanges="keyboardHidden|orientation|screenSize|screenLayout"
        android:exported="true"
        android:label="place"
        android:permission="com.example.permission" // Sets a permission
        android:theme="@style/Theme.AppCompat.DayNight.NoActionBar">
        <intent-filter>

            <action android:name="com.action.places" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

    // Activity 2: With no permission, exported = true

      <activity
        android:name="userCheck"
        android:configChanges="mcc|mnc|keyboard|keyboardHidden|orientation"
        android:exported="true"
        android:theme="@style/Theme.AppCompat.DayNight.NoActionBar">
        <intent-filter>
            <action android:name="com.confirm.passowrd" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
     </activity>

Now, i want to move to single activity.

   // Single activity: exported = true. How can i handle permission for place action?
    <activity
        android:name="com.example.singleActivity"
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <!-- Place -->
            <action android:name="com.action.places"/> // How can i handle permission for this action?

            <!-- UserCheck -->
            <action android:name="com.confirm.passowrd" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
   

Solution

  • You can't use manifest-based permissions if you want to restrict a single Activity from being launched in some cases but not others. The manifest-based permission controls access to the specific Activity, no matter how it is being launched.

    What you could do is create an <activity-alias> for your single Activity. An <activity-alias> is a manifest entry that references an actual Activity, but provides alternate launch options. You can specify a different set of permissions on the <activity-alias> than the ones you have on the actual Activity. You can also specify different launchMode, different <intent-filter>, etc.