質問

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>
役に立ちましたか?

解決

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.

他のヒント

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top