一般情況下,app在首次啟動時需要加載大量布局跟數據,我公司項目首次啟動需要加載一個Activity里面有7個Fragment…加上各種數據,經常導致啟動時會有白屏現象,為了完美解決各種現象也花了一些功夫,這里記錄一下. 1:點擊App啟動時第一個白屏 這個白屏是因為主題背景設置導致的,解決也比較簡單,就是設置App主題背景圖片為自己App 加載的圖片即可,這里注意要把加載Activity跟布局的背景去掉,不然主題的背景會被覆蓋!
<!-- application theme. --><style name="A然后在清單文件設置主題就可以!2:splash 界面過后,跳轉主界面時間過長,出現白屏; 這種情況就是主界面布局復雜,加載時間長導致的或者主界面加載過程有耗時操作;這里不討論耗時操作的影響,有點經驗都不會放到主線程; 復雜的布局改怎么處理?總不能不加載吧… 這里使用延時加載子布局方法 以前我們的思路是先加載splash界面,一定時間后再跳轉到主界面,這個思路有2個缺點 第一: 加載splash界面時不能加載網絡數據(加載了也要做緩存,因為這個界面很快就要被回收),浪費了中間的寶貴時間 第二:加載splash界面時不能預加載復雜的主界面,導致跳轉到主界面后卡頓,白屏! 這里的思路是將 splash界面圖片直接放在主界面,用延時加載布局方法先加載圖片后再加載復雜布局; 下面上代碼
import java.lang.ref.WeakReference;import android.content.Context;import android.os.Bundle;import android.os.Handler;import android.support.v4.app.FragmentActivity;import android.support.v4.app.FragmentTransaction;import android.view.ViewStub;public class MainActivity extends FragmentActivity { PRivate Handler mHandler = new Handler(); private SplashFragment splashFragment; private ViewStub viewStub; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); splashFragment = new SplashFragment(); FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.frame, splashFragment); transaction.commit();// mHandler.postDelayed(new Runnable() {// @Override// public void run() {// mProgressBar.setVisibility(View.GONE);// iv.setVisibility(View.VISIBLE);// }// }, 2500); viewStub = (ViewStub)findViewById(R.id.content_viewstub); //1.判斷當窗體加載完畢的時候,立馬再加載真正的布局進來 getWindow().getDecorView().post(new Runnable() { @Override public void run() { // 開啟延遲加載 mHandler.post(new Runnable() { @Override public void run() { //將viewstub加載進來 viewStub.inflate(); } } ); } }); //2.判斷當窗體加載完畢的時候執行,延遲一段時間做動畫。 getWindow().getDecorView().post(new Runnable() { @Override public void run() { // 開啟延遲加載,也可以不用延遲可以立馬執行(我這里延遲是為了實現fragment里面的動畫效果的耗時) mHandler.postDelayed(new DelayRunnable(MainActivity.this, splashFragment) ,2000);// mHandler.post(new DelayRunnable()); } }); //3.同時進行異步加載數據 } @Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); } static class DelayRunnable implements Runnable{ private WeakReference<Context> contextRef; private WeakReference<SplashFragment> fragmentRef; public DelayRunnable(Context context, SplashFragment f) { contextRef = new WeakReference<Context>(context); fragmentRef = new WeakReference<SplashFragment>(f); } @Override public void run() { // 移除fragment if(contextRef!=null){ SplashFragment splashFragment = fragmentRef.get(); if(splashFragment==null){ return; } FragmentActivity activity = (FragmentActivity) contextRef.get(); FragmentTransaction transaction = activity.getSupportFragmentManager().beginTransaction(); transaction.remove(splashFragment); transaction.commit(); } } }}簡單的Fragmemt
import android.os.Bundle;import android.support.annotation.Nullable;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;public class SplashFragment extends Fragment { @Override @Nullable public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_splash, container,false); }}布局activity_main
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.applicationstartoptimizedemo.MainActivity" > <ViewStub android:id="@+id/content_viewstub" android:layout="@layout/activity_main_viewstub" android:layout_width="match_parent" android:layout_height="match_parent"/> <FrameLayout android:id="@+id/frame" android:layout_width="match_parent" android:layout_height="match_parent" > </FrameLayout></RelativeLayout>activity_main_viewstub
<?xml version="1.0" encoding="utf-8"?><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/content" android:layout_width="match_parent" android:layout_height="match_parent" > <ProgressBar android:id="@+id/progressBar1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" /> <ImageView android:id="@+id/iv" android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="fitStart" android:src="@drawable/content" /></FrameLayout>fragment_splash
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.applicationstartoptimizedemo.MainActivity" > <FrameLayout android:id="@+id/frame" android:background="@drawable/splash12" android:layout_width="match_parent" android:layout_height="match_parent" > </FrameLayout></RelativeLayout>主要就是用到 ViewStub 延時加載就機制.
新聞熱點
疑難解答