android 預加載進程(頁面跳轉白屏或黑屏)

現象:
第一次從一個進程的activity跳轉到另一個進程的activity,會先呈現出黑屏(或白屏)的現象,然後纔是第二個activity的界面。這是因爲第一次跳轉的時候,需要先啓動另一個進程,而啓動進程需要消耗一定的時間,而在這時間內會直接顯示window的背景(黑色或者白色),因此會出現黑屏或者白屏的現象。

解決辦法:
在跳轉之前,預加載進程,從而避免啓動進程的時間。
如我在某個界面啓動service,而該service在AndroidManifest.xml設置爲想要開啓的進程,這個用來預加載進程的service不需要實現什麼功能,只要存在即可。

AndroidManifest.xml:

<service
    android:name="com.eebbk.pointread.HideService"
    android:process=":pointread"/>

HideService:

package com.eebbk.pointread;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;

/**
 * Created by zhangshao on 2016/8/3.
 *
 * 只是單純的爲了預加載點讀進程,無其他意義
 */
public class HideService extends Service{
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }


}

我在要跳轉到另一個進程的activity中添加開啓和停止服務的方法:

/**
     * 開始預加載進程
     */
    private void startHideService(){
        Intent intent = new Intent(this, HideService.class);
        this.startService(intent);
    }

    private void stopHideService(){
        Intent intent = new Intent(this, HideService.class);
        this.stopService(intent);
    }

在該activity的OnCreate()中調用:startHideService();
在該activity的OnDestroy()中調用:stopHideService();

總結
預加載並不需要非得是Service,只要是看不見的組件就行,比如用廣播。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章