当前位置: 首页 > 工具软件 > Black Screen > 使用案例 >

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

周楷
2023-12-01

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











 类似资料: