Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Acitivity SingleTask behaviour with SplashScreen

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>
like image 806
Denny1989 Avatar asked Jan 11 '23 14:01

Denny1989


1 Answers

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.

like image 113
Sripathi Avatar answered Jan 19 '23 05:01

Sripathi