Home > Software engineering >  onBackPressed() from deep link exit the app
onBackPressed() from deep link exit the app

Time:08-28

I just start with Android with Java and I set Deep link when I open it from closed app I didn't see the splash screen and when I hit back button its exit the app how can I make it if its from deep link launch of the app to show splash screen and more important when I press back arrow to lead to Home screen keep in mind that activity is also accessible form another fragments so its need to be explicit from deep link to Home activity Here is my XML file:

<application
    android:hardwareAccelerated="true"
    android:allowBackup="true"
    android:dataExtractionRules="@xml/data_extraction_rules"
    android:fullBackupContent="@xml/backup_rules"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/Theme.FoodRecipeAppProject"
    tools:targetApi="31">

    <activity
        android:name=".SplashActivity"
        android:exported="true">

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

        <intent-filter android:label="url">
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
            <data
                android:scheme="https"
                android:host="www.example.com"
                android:pathPrefix="/recipe" />
        </intent-filter>

    </activity>


    <activity
        android:windowSoftInputMode="stateAlwaysHidden|adjustPan"
        android:name=".DashBoardActivity"
        android:exported="false" />

    <activity
        android:name=".MainActivity"
        android:exported="false">

    </activity>


    <meta-data
        android:name="preloaded_fonts"
        android:resource="@array/preloaded_fonts" />
</application>

CodePudding user response:

You have to declare parentActivityname on your manifest like this

<activity
    android:name=".RecepieActivity"
    android:parentActivityName=".YourParentActivity"
    android:exported="true">

    <intent-filter android:label="url">
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data
            android:scheme="https"
            android:host="www.example.com"
            android:pathPrefix="/recipe" />
    </intent-filter>

</activity>

and then override onBackPressed function in your activity class like this

 @Override
 public void onBackPressed(){
     NavUtils.navigateUpFromSameTask(this);
 }

This will make you back to parent activity when back button is pressed

CodePudding user response:

Instead of using an activity just to show splash screen, use SplashScreen API directly in the activity that is being deep linked. Alternatively, you can also setup a routing activity which will handle all incoming deep link requests and show splash screen, after which it'll redirect to the activity defined in deep link.

  • Related