我使用主要活动以外的活动以及在主要活动中使用对话框,以两种不同的方式实现了启动画面。
使用活动在从启动切换到主活动期间会有明显的延迟,这看起来不太好。 所以我尝试使用Dialogs。代码粘贴在下面。它只是显示启动画面并保持在那里。它不会忽略对话框并显示主要布局。
package com.example.splashscreen;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.app.Dialog;
import android.util.Log;
import android.view.Menu;
public class MainActivity extends Activity {
Dialog splashDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
showSplashScreen();
setContentView(R.layout.activity_main);
Log.i("dismiss dialog............ 1", "");
new InitializeTask().execute();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
/**
* Shows the splash screen over the full Activity
*/
protected void showSplashScreen() {
splashDialog = new Dialog(this, R.style.SplashScreen);
splashDialog.setContentView(R.layout.splashscreen);
splashDialog.setCancelable(false);
splashDialog.show();
}
final class InitializeTask extends AsyncTask<Void, Void, Void>
{
protected void onPreExecute()
{
}
protected void onPostExecute() { // Dismiss dialog here
Log.i("dismiss dialog............", "");
splashDialog.dismiss();
}
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
return null;
}
}
}
代码显示启动画面但不显示列表,它显示空列表。但是当包含textview时,activity_main正在工作(mainactivity从Activity而不是ListActivity扩展)。现在,activity_main布局有一个带有字符串数组条目的列表视图。请允许任何人告诉我为什么列表没有填充。
package com.ngn.bcma;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.app.Dialog;
import android.app.ListActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
public class MainActivity extends ListActivity {
Dialog splashDialog;
private boolean isSplashShown = false;
public static final String VALUE_KEY = "splashShown";
//ListView mainMenu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
restoreValue(savedInstanceState);
// mainMenu = getListView();
Log.i("onCreate ................ ", "splash shown : "+isSplashShown);
if (isSplashShown) {
setContentView(R.layout.activity_main);
// Rebuild your UI with your saved state here
// String[] listItems = {"exploring", "android",
// "list", "activities"};
//
// ArrayAdapter adapter = new ArrayAdapter (this
// ,android.R.layout.simple_list_item_1, listItems);
// setListAdapter(adapter);
}
else {
showSplashScreen();
setContentView(R.layout.activity_main);
// Do your heavy loading here on a background thread
}
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
restoreValue(savedInstanceState);
Log.i("onRestoreInstanceState ................ ", "splash shown : "+isSplashShown);
// TODO Auto-generated method stub
super.onRestoreInstanceState(savedInstanceState);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
Log.i("onSaveInstanceState ................ ", "splash shown : "+isSplashShown);
outState.putBoolean("VALUE_KEY", isSplashShown); // TODO: move to strings
// TODO Auto-generated method stub
super.onSaveInstanceState(outState);
}
private void restoreValue(Bundle savedInstanceState){
if (savedInstanceState != null && savedInstanceState.containsKey(VALUE_KEY))
{
isSplashShown = savedInstanceState.getBoolean(VALUE_KEY);
}
}
/**
* Shows the splash screen over the full Activity
*/
protected void showSplashScreen() {
splashDialog = new Dialog(this, R.style.SplashScreen);
splashDialog.setContentView(R.layout.splashscreen);
splashDialog.setCancelable(false);
splashDialog.show();
isSplashShown = true;
// Set Runnable to remove splash screen just in case
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
// new InitializeTask().execute();
removeSplashScreen();
}
}, 2000); // TODO: move to res
}
/**
* Removes the Dialog that displays the splash screen
*/
protected void removeSplashScreen() {
if (splashDialog != null) {
splashDialog.dismiss();
splashDialog = null;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
答案 0 :(得分:3)
可能onPostExecute
方法未调用,只需将@Override
放在onPostExecute
之前,以确保您将AsyncTask onPostExecute方法重写为:
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
Log.i("dismiss dialog............", "");
if(splashDialog.isShowing())
splashDialog.dismiss();
}