如何避免Activity切换时的黑屏(Black Screen/Blank Screen)

1. 简介

    问题描述:how to avoid blank screen between activities android?

2. 解决方案

2.1 显示Loading画面

    在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);
      }
}

   3) 启动LoadingScreenActivity

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);
}











评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值