4

In my App I have:

-SplashScreen (SSc) preparing the application (starting services and so on) -MainActivity (MA) the most relevant part of the app handling most actions -and some other activities which are not that relevant

For my App I'd like to have the behavior like launchMode singleTask, so that my App is always started as a new Task, even when opened through a link click in SMS/EMail app. The best would be to have only one Instance of my Activities as they are all serially navigable.

However when I start SSc as singleTask it is the root of the stack and navigating to the MainActivity, pressing home, click on the Launcher icon again the app is fully restarted. So SSc is shown again and so on. In this Situation, I would like the MainActivty to be brought to the front instead.

my wish would be: launcherclick -> SSc ->MA ->HOME -> launcherclick -> bring MA to front -> HOME-> relaunch from recents -> bring MA to front

Click on link ->SSc/MA (whether it is first start) with the same instances

In my App it does not make sense to have multiple instances, as the background service only handles one MainActivity at a time because it polls data frequently just for the seen "Thing".

Do you have any recommendations to achieve this goal?

my first idea was a LauncherActivity with launchMode singletask without layout to route the intents to the other activities (which most likely will be singleTop !?, because its only in one task then) like:

public class LauncherActivity extends Activity {
 private boolean firstStart = true;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if(firstStart){
            startActivity(new Intent(this, SplashScreen.class));
            firstStart = false;
        } else {
            Intent i = new Intent(this, MainActivity.class);
            i.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
            startActivity(i);
        }
    }
}

Manifest xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="x.startintenttest">

    <application
        android:allowBackup="true"
        android:allowTaskReparenting="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name">
        <activity
            android:name="x.startintenttest.MainActivity"
            android:label="@string/app_name"
            android:launchMode="singleTop"></activity>
        <activity
            android:name="x.startintenttest.MainActivity2"
            android:label="@string/title_activity_main_activity2"></activity>
        <activity
            android:name="x.startintenttest.SplashScreen"
            android:label="@string/title_activity_splash_screen"
            android:launchMode="singleTask">
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />

                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />

                <data
                    android:host="*.xyz.de"
                    android:pathPattern="/...-........."
                    android:scheme="https" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

        </activity>
        
    </application>

</manifest>
Anice Jahanjoo
  • 7,088
  • 3
  • 20
  • 29
Denny1989
  • 639
  • 1
  • 9
  • 15
  • Can you please post your manifest file? – Chaosit May 14 '14 at 07:31
  • Does this happen every time? Or only when you start the application from the installer or from an IDE (Eclipse, IntelliJ, etc.). Try to force close your application and then start it from the list of available apps and see if the problem goes away. If it does, then you are just seeing this nasty Android bug: http://stackoverflow.com/a/16447508/769265 – David Wasser May 14 '14 at 17:02

2 Answers2

9

Its simple, I also did the same. I was using singleTask for my splash and the main activity. So that I faced the same issue(Splash was showing at every wakeup). But I solved this by removing the singleTask from the splash and kept it for the MA alone(I used to finish the splashActivity when the MA starts). Try this trick.

Sripathi
  • 1,760
  • 15
  • 20
  • thank you. i will use this approach in our app. it is the closest to our needs. – Denny1989 May 15 '14 at 09:10
  • @SudoPlz in Androidmanifest put android:launchmode="singleTask" on your main activity, instead of on your SplashScreen. That's it pretty much. – Mikkel Larsen Jul 11 '18 at 10:22
  • Thank you so much. However this solution leaves the splash screen as the app preview for the app that launched the click event in the recent apps screen, since it's launched in the same task as the caller activity. Does anyone have a solution for that? I've been wondering if I should make a new link-handling-activity with launchMode="singleTask" just to fix that issue and then redirect to the usual splashscreen, which in turn would open the main activity. This, in theory, would alleviate the app preview issue and the splash screen OP was having. – Gonzalo Oct 18 '18 at 16:35
0

The solution provided Sripathi fixes the issue, but you end up with the link-opening-app having the splash screen layout in its preview. My solution to this is to have a LinkEntryPointActivity with no layout that then delegates the received intent to the splash screen.

class LinkEntryPointActivity : MyBaseActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val intentClone = intent.clone() as Intent
        val componentName = intentClone.component
        val newComponentName = ComponentName(componentName.packageName, SplashActivity::class.java.name)
        intentClone.component = newComponentName
        startActivity(intentClone)
        finish()
    }
}

And declare it in AndroidManifest.xml as

<activity android:name=".ui.LinkEntryPointActivity"
    android:launchMode="singleTask"   <-- This forces to open the link in a new task
    android:screenOrientation="portrait">
    <!-- Intent filters here -->
</activity>

The original splash screen activity should still be handling the MAIN action in the LAUNCHER category as usual.

Gonzalo
  • 3,674
  • 2
  • 26
  • 28