问题描述:how to avoid blank screen between activities android?
在onCreate中做尽量少的事情,如果在启动时需要做大量的工作,则把上工作放于后台thread中去做,而在onCreate中仅弹出一个进度Dialog以通知用户应用正在Loading。当后台线程把工作做完时,则dismiss dialog并load正常的View。
显示Loading画面2.5秒之后再显示真正的画面。例子代码如下:
1)loading_screen.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:gravity="center" android:orientation="vertical"
android:layout_height="fill_parent" android:background="#E5E5E5">
<TextView android:text="Please wait while your data gets loaded..."
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textColor="#000000">
</TextView>
<ProgressBar android:id="@+id/mainSpinner1" android:layout_gravity="center"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:indeterminate="true"
style="?android:attr/progressBarStyleInverse">
</ProgressBar>
</LinearLayout>
2) LoadingScreenActivity.java
2500ms之后,Loading下一个Activity。
public class LoadingScreenActivity extends Activity {
//Introduce an delay
private final int WAIT_TIME = 2500;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
System.out.println("LoadingScreenActivity screen started");
setContentView(R.layout.loading_screen);
findViewById(R.id.mainSpinner1).setVisibility(View.VISIBLE);
new Handler().postDelayed(new Runnable(){
@Override
public void run() {
//Simulating a long running task
this.Sleep(1000);
System.out.println("Going to Profile Data");
/* Create an Intent that will start the ProfileData-Activity. */
Intent mainIntent = new Intent(LoadingScreenActivity.this,ProfileData.class);
LoadingScreenActivity.this.startActivity(mainIntent);
LoadingScreenActivity.this.finish();
}
}, WAIT_TIME);
}
}
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Intent intent = new Intent(ProfileList.this, LoadingScreenActivity.class);
startActivity(intent);
}