@@ -8,10 +8,32 @@ import android.os.Bundle
88import android.util.Log
99
1010class RedirectActivity : Activity () {
11+
12+ private var hasRedirected = false
13+
1114 override fun onCreate (savedInstanceState : Bundle ? ) {
1215 super .onCreate(savedInstanceState)
16+ hasRedirected = false
17+ handleIntent(intent, isReEntry = false )
18+ }
1319
14- val incomingIntent = intent
20+ override fun onNewIntent (intent : Intent ) {
21+ super .onNewIntent(intent)
22+ setIntent(intent)
23+
24+ // If we already redirected and the browser is on top of us,
25+ // this is a re-entry from the launcher icon or recents.
26+ // Just do nothing — the browser activity is still on top of our back stack.
27+ if (hasRedirected) {
28+ Log .d(" Redirector" , " Re-entry detected, skipping redirect (browser is already on top)" )
29+ return
30+ }
31+
32+ handleIntent(intent, isReEntry = true )
33+ }
34+
35+ private fun handleIntent (incomingIntent : Intent , isReEntry : Boolean ) {
36+ val action = incomingIntent.action
1537 var data = incomingIntent.data
1638
1739 // WebAPK specific extras might contain the URL
@@ -25,44 +47,46 @@ class RedirectActivity : Activity() {
2547 if (data != null ) {
2648 Log .d(" Redirector" , " Redirecting URL: $data " )
2749
28- // Get target package from preferences
2950 val prefs = getSharedPreferences(" RedirectorPrefs" , Context .MODE_PRIVATE )
3051 val targetPackage = prefs.getString(" target_package" , null )
3152
3253 if (targetPackage == null ) {
33- // If not set, go to selection screen
3454 val selectIntent = Intent (this , SettingsActivity ::class .java)
35- selectIntent.addFlags(Intent .FLAG_ACTIVITY_NEW_TASK )
3655 startActivity(selectIntent)
37- finish()
3856 return
3957 }
4058
41- // Create a new intent to the target browser
4259 val targetIntent = Intent (Intent .ACTION_VIEW , data)
4360 targetIntent.setPackage(targetPackage)
44- targetIntent.addFlags( Intent . FLAG_ACTIVITY_NEW_TASK )
61+ // NO FLAG_ACTIVITY_NEW_TASK — launch browser within our task
4562
46- // Copy all extras from the original intent
4763 val extras = incomingIntent.extras
4864 if (extras != null ) {
4965 targetIntent.putExtras(extras)
5066 }
5167
5268 try {
5369 startActivity(targetIntent)
70+ hasRedirected = true
71+ // NO finish() — keep our activity in the back stack
72+ // so the task stays alive in Recents
5473 } catch (e: Exception ) {
5574 Log .e(" Redirector" , " Failed to start target activity" , e)
56- val selectIntent = Intent (this , SettingsActivity ::class .java)
57- startActivity(selectIntent)
75+ // Browser might require NEW_TASK (singleTask browsers), try with it
76+ targetIntent.addFlags(Intent .FLAG_ACTIVITY_NEW_TASK )
77+ try {
78+ startActivity(targetIntent)
79+ hasRedirected = true
80+ } catch (e2: Exception ) {
81+ Log .e(" Redirector" , " Fallback also failed" , e2)
82+ val selectIntent = Intent (this , SettingsActivity ::class .java)
83+ startActivity(selectIntent)
84+ }
5885 }
59- } else {
60- Log .w(" Redirector" , " No data or webapp_url found in intent" )
61- // If opened directly without data, show selection screen
86+ } else if (action == Intent .ACTION_MAIN ) {
6287 val selectIntent = Intent (this , SettingsActivity ::class .java)
6388 startActivity(selectIntent)
6489 }
65-
66- finish()
90+ // NO finish() anywhere
6791 }
6892}
0 commit comments