How to make splash screen not to load if app is already in memory

喜夏-厌秋 提交于 2019-12-13 20:16:15

问题


I have some troubles with spash screen. When I launch app, splash screen activity launches for some seconds. After it main activity launches.

And if i press home button on the main activity and then relaunch app from apps list, splash activity launches again although the app is already in backstack. But I expect main activity to restore from memory.

And if i press back button after that android returns me to the previous copy of main activity.

What I have to do to make splash screen appear just once? And how to get my app relaunched from the last screen I saw before the home button was clicked?

    <activity
            android:name=".ui.SplashActivity"
            android:noHistory="true"
            android:screenOrientation="portrait"
            android:label="@string/app_name">

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

    <activity android:name=".ui.MainActivity"/>

回答1:


This is a design problem. Your launcher activity should not be your splash screen activity. Instead, open your splash activity in your main activity's onCreate method. That way, if it is opened fresh, onCreate is called and the splash screen is shown. Otherwise, if the app is merely resumed, which calls onResume, there would be no call to open the splash screen activity.

Then you can change your manifest to this:

<activity
        android:name=".ui.MainActivity"
        android:noHistory="true"
        android:screenOrientation="portrait"
        android:label="@string/app_name">

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

<activity android:name=".ui.SplashActivity"/>


来源:https://stackoverflow.com/questions/13112329/how-to-make-splash-screen-not-to-load-if-app-is-already-in-memory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!