Android启动屏和引导页

启动app的时候闪黑屏?

可以用自己app的logo来代替黑屏,且看如下配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
<!-- res/drawable/welcome_bg -->
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<color android:color="@color/colorPrimary" />
</item>
<item>
<bitmap
android:gravity="center"
android:src="@mipmap/screen"
android:tileMode="disabled" />
</item>
</layer-list>
1
2
3
4
<!-- add below code to your style.xml -->
<style name="WelcomeAppTheme" parent="@android:style/Theme.DeviceDefault.NoActionBar.Fullscreen">
<item name="android:windowBackground">@drawable/welcome_bg</item>
</style>

预加载主内容

为了在展示欢迎页的时候提前加载,我们调换下MainActivity和WelcomeActivity顺序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<!-- add below code to your AndroidMainfest.xml -->
<activity
android:name=".MainActivity"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale"
android:launchMode="singleTop"
android:screenOrientation="portrait"
android:theme="@style/AppTheme">
<intent-filter android:label="@string/app_name">
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity
android:name=".WelcomeActivity"
android:exported="true"
android:screenOrientation="portrait"
android:theme="@style/WelcomeAppTheme" />
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
toNext();

initData();
initView();
}
private void toNext() {
if (MyApplication.sSkipWelcome) {
return;
}
MyApplication.sSkipWelcome = true;

Intent intent = new Intent(this, WelcomeActivity.class);
startActivity(intent);
overridePendingTransition(0, 0); // 禁用动画
}

可以参考: https://github.com/lyloou/lou/blob/demo/test/src/main/AndroidManifest.xml

启动已经存在的activity

  • android - Resume the Activity instead of Starting if already exists in back stack - Stack Overflow
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    You can add this two lines and try:

    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    Write this in your manifest file inside Activity

    <activity
    android:name=".SettingsActivity"
    android:launchMode="singleInstance"
    android:screenOrientation="portrait" >
    </activity>

    "singleTask" and "singleInstance" activities can only begin a task. They are always at the root of the activity stack. Moreover, the device can hold only one instance of the activity at a time — only one such task.

    You can use SingleTask or SingleInstance

    "singleTask" - The system creates the activity at the root of a new task and routes the intent to it. However, if an instance of the activity already exists, the system routes the intent to existing instance through a call to its onNewIntent() method, rather than creating a new one.

    "singleInstance" - Same as "singleTask", except that the system doesn't launch any other activities into the task holding the instance. The activity is always the single and only member of its task.

    Refer this link http://developer.android.com/guide/topics/manifest/activity-element.html

可以参考: https://github.com/lyloou/lou/blob/demo/test/src/main/java/com/lyloou/test/WelcomeActivity.java