Android - URI Scheme - Stop from opening multiple instances of application -
i'm using custom uri scheme redirect user website application, when launching app, opens new instance instead of restoring open one.
i have tried both using launchmode="singletask" or "singleinstance" don't seem affect it.
<!-- splash. --> <activity android:launchmode="singletask" android:name=".activities.splash" android:label="@string/app_name" android:screenorientation="portrait" android:windowsoftinputmode="statehidden" > <intent-filter> <data android:scheme="customscheme" /> <action android:name="android.intent.action.view"/> <category android:name="android.intent.category.browsable" /> <category android:name="android.intent.category.default"/> </intent-filter> <intent-filter> <action android:name="android.intent.action.main"/> <category android:name="android.intent.category.launcher"/> </intent-filter> </activity>
example:
user opens application, activity goes activity b .
user opens website, , gets redirected app via uri.
application on activity a.
user presses button, destroying activity a.
activity b present first instance.
thanks time , help!
assuming you've not set android:taskaffinity
activity b, doing correct thing.
- when launch app starts activitya
- activitya launches activityb same task (even if set
launchmode="singleinstance"
activityb, android ignore since has sametaskaffinity
activitya) - user opens website , redirected app. android brings existing task application (containing activitya , activityb) foreground , creates new instance of activitya , puts on top of existing activies. have task stack activitya -> activityb -> activitya.
- user presses button, finishes instance of activitya on top of stack , reveals instance of activityb underneath.
if think this, android cannot open existing instance of activitya because covered activityb.
there multiple ways of getting around this, depending on how want app behave in different circumstances. let's assume that, if app running, want task stack cleared root instance of activitya , want reuse instance (and not create one). 1 of easiest ways have web browser open activity (not activitya), let's call redirectactivity
. new activity has following code in oncreate()
:
super.oncreate(...); intent intent = new intent(this, activitya.class); intent.addflags(intent.flag_activity_clear_top | intent.flag_activity_single_top); startactivity(intent); finish();
this code 1 of following, depending...
- if app not running, create new instance of activitya.
- if app running, clear task stack of activities may on top of existing instance of activitya , return existing instance of activitya.
note: there other behaviours might appropriate , other techniques can use similar behaviour.
Comments
Post a Comment