12/06/2024:
My issue specifically was caused by scanning the QR code with the native QR reader app on my phone. Switching to a different QR reader app and scanning actually causes my app to open correctly.
I am developing an android app using the Avalonia UI framework. I believe this is unrelated to the actual problem, but I don't know for sure.
I've added an activity to my project, so the app will launch when I click a certain link or scan a QR code. The app opens correctly when I use a link or scan a QR code, but:
I couldn't find anything regarding this odd behaviour, so I'm hoping for answer here.
My goal is that the icon and the display name remain constant and don't change to something else.
This behaviour only happens when the app starts through the activity. Manually starting the app causes no issues or odd behaviours.
My activity class in C# :
[Activity(Label = "MyApp", Name = "com.Test.MyApp.StartAppActivity", MainLauncher = false)]
public class StartAppActivity : Activity
{
protected override void OnCreate(Bundle? savedInstanceState)
{
base.OnCreate(savedInstanceState);
this.StartActivity(typeof(MainActivity));
}
}
The code inside my AndroidManifest.xml:
<application android:label="MyApp"
android:theme="@style/SplashScreen_Theme"
android:icon="@mipmap/launcher_foreground"
android:logo="@mipmap/launcher_foreground" >
<activity android:name="com.Test.MyApp.StartAppActivity"
android:icon="@mipmap/launcher_foreground"
android:exported="true"
android:label="MyApp">
<intent-filter android:label="MyApp" android:icon="@mipmap/launcher_foreground">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="MyAppScheme" />
</intent-filter>
</activity>
</application>
It sounds like you're only experiencing this behaviour when deep-linking into your application meaning Google Play Services is likely creating an intent to open your StartAppActivity
when you click the link/scan the QR code and is therefore retained as the parent task. You should look into Android Launch Modes, which will tell the Android OS how you would like your activity to open (whether that's through an intent like a deep-link or via the launcher).
Assuming that I have understood your problem correctly, adding launchMode="singleTask"
to your AndroidManifest.xml
under the Activity
tag should resolve the problem, like so:
<activity android:name="com.Test.MyApp.StartAppActivity"
android:icon="@mipmap/launcher_foreground"
android:exported="true"
android:launchMode="singleTask"
android:label="MyApp">
I would also recommend, if possible that you should try to use the onNewIntent()
override in your MainActivity
rather than having StartAppActivity
launch your MainActivity
, this should work very well with the singleTask
launch mode. You can read more about that here.