0

我使用除主要活动以外的活动和在主要活动中使用对话框以两种不同的方式实现了启动画面。

在从启动到主要活动的切换过程中,使用活动有一个明显的延迟,这看起来不太好。所以我尝试使用对话框。代码粘贴在下面。它只显示启动画面并停留在那里。它不会关闭对话框并显示主要布局。

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

    }
}

代码显示启动画面但不显示列表,它显示空列表。但是当它包含一个文本视图时,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;
    }

}
4

1 回答 1

3

可能onPostExecute没有调用方法,所以只是放在@Override前面onPostExecute以确保您将 AsyncTask onPostExecute 方法覆盖为:

@Override
protected void onPostExecute(Void result) {
    super.onPostExecute(result);
    Log.i("dismiss dialog............", "");
    if(splashDialog.isShowing())
        splashDialog.dismiss(); 
  }
于 2013-05-24T06:55:33.067 回答