Usually, app needs to load a lot of layout and data when it is first started. Our company needs to load an Activity with 7 fragments in it when it is first started. Add all kinds of data, often lead to white screen phenomenon at startup. In order to solve all kinds of phenomena perfectly, some efforts have been spent. Here is a record.
1: Click on the first white screen when App starts
This white screen is due to the theme background settings, the solution is relatively simple, that is to set the background image of the theme of App to load the image for their own App, here pay attention to the loading Activity and layout of the background removed, otherwise the background of the theme will be covered! ________
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:windowNoTitle">true</item>
<item
<!-- Custom Background -->name="android:windowBackground">@color/white</item>
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
Then set the theme in the manifest file!
2: After splash interface, the time of jumping the main interface is too long and white screen appears.
This situation is that the layout of the main interface is complex and the loading time is long, or the loading process of the main interface is time-consuming. Without discussing the impact of time-consuming operation, some experience will not be put in the main thread.
How to deal with the complicated layout? You can't help loading it.
Here we use the delayed load sublayout method
In the past, our idea was to load splash interface first and then jump to the main interface after a certain period of time. This idea has two shortcomings.
Firstly, when loading splash interface, network data can not be loaded (caching should be done when loading, because the interface will soon be recycled), which wastes precious time in the middle.
Second: When loading splash interface, the complex main interface can not be preloaded, leading to jump to the main interface after the carton, white screen!
The idea here is to put the splash interface pictures directly in the main interface, and load the pictures first and then the complex layout with the method of delayed loading layout.
The following code
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. Judge that when the form is loaded, it immediately loads the real layout.
getWindow().getDecorView().post(new Runnable() {
@Override
public void run() {
// Open Delayed Loading
mHandler.post(new Runnable() {
@Override
public void run() {
//Load viewstub in
viewStub.inflate();
}
} );
}
});
//2. Judge that when the form is loaded, it will be executed and the animation will be delayed for a period of time.
getWindow().getDecorView().post(new Runnable() {
@Override
public void run() {
// Turn on delayed loading, or you can execute it without delay (I'm delaying here to achieve the animation effect in fragment s)
mHandler.postDelayed(new DelayRunnable(MainActivity.this, splashFragment) ,2000);
// mHandler.post(new DelayRunnable());
}
});
//3. Simultaneously loading data asynchronously
}
@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() {
// Remove 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();
}
}
}
}
Simple 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);
}
}
Layout 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>
The main purpose is to use the delayed loading mechanism of ViewStub.