简体   繁体   中英

Splash-screen approach in single Activity app

I am trying to decide on the best approach for creating an app splash-screen while taking into consideration Google's latest recommendations on choosing a single Activity app whenever possible.

See here:

"The new approach is to use one-activity structure whenever possible."

and here:

"Today we are introducing the Navigation component as a framework for structuring your in-app UI, with a focus on making a single-Activity app the preferred architecture."

Any good splash-screen approaches I have found have a dedicated Activity for the splash screen:

See here

and here

Has anyone else had any experience creating a splash screen in a single Activity app? Does the the single Activity recommendation include the splash-screen or is it a special case? Does anyone have any good examples or advice on this?

Cheers, Paul.

The approach I use is the following:

First define a drawable for the background:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:drawable="@color/green"/>

    <item>
        <bitmap
            android:gravity="center"
            android:src="@mipmap/ic_launcher"/>
    </item>

</layer-list>

2. Define a new style to use in the splashScreen:

<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
    <item name="android:windowBackground">@drawable/background_splash</item>
</style>

3. Make your activity implement use the splash theme:

<activity
    android:name=".MainActivity"
    android:theme="@style/SplashTheme">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

4. In on create, before the super invocation and before the set content view set the default app theme:

override fun onCreate(savedInstanceState: Bundle) {
    setTheme(android.R.style.AppTheme)
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main);
}

This approach is the one I've been using even with multiple Activities, since it follows the guidelines laid down by google: itshows the splash right away and doesn't stay longer than needed.

If you are using ConstraintLayout in your layouts, you can use the Group class of Android to group multiple views. Please refer to the following link for more information.

https://developer.android.com/reference/android/support/constraint/Group

This class controls the visibility of a set of referenced widgets. Widgets are referenced by being added to a comma separated list of ids, eg:

 <android.support.constraint.Group
          android:id="@+id/group"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:visibility="visible"
          app:constraint_referenced_ids="button4,button9" />

FYI - Multiple groups can reference the same widgets -- in that case, the XML declaration order will define the final visibility state (the group declared last will have the last word).

Hope this helps you resolve the issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM