简体   繁体   English

Android启动画面

[英]Android Splash screen

I have implemented splash screen in 2 different ways using activity other than main activity and using dialog in main activity. 我已经通过2种不同的方式使用初始活动(除了主要活动)和主要活动中的对话框来实现了启动画面。

Using activity there is a noticeable delay during switching from splash to main activity which is not looking good. 使用活动时,从初始活动切换到主要活动的过程中会有明显的延迟,这看起来并不好。 So I tried using Dialogs. 所以我尝试使用对话框。 The code is pasted below. 该代码粘贴在下面。 It just shows the splash screen and stays there. 它仅显示启动屏幕并停留在该屏幕上。 Its not dismissing the dialog and showing main layout. 它不关闭对话框并显示主要布局。

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

    }
}

The code shows splash screen but not the list, it shows up the empty list. 该代码显示初始屏幕,但不显示列表,它显示空白列表。 But activity_main when it includes a textview was working (mainactivity being extended from Activity instead of ListActivity). 但是activity_main包含文本视图时正在运行(mainactivity是从Activity而不是ListActivity扩展的)。 Now activity_main layout has a listview with string array entries. 现在activity_main布局具有一个包含字符串数组条目的列表视图。 Please can anyone let me know why the list is not populated. 请任何人让我知道为什么未填充列表。

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

}

probably onPostExecute method not called so just put @Override before onPostExecute to make sure you are Overriding AsyncTask onPostExecute method as : 大概onPostExecute不叫方法,因此只是把@Override之前onPostExecute ,以确保您要重写的AsyncTask onPostExecute方法为:

@Override
protected void onPostExecute(Void result) {
    super.onPostExecute(result);
    Log.i("dismiss dialog............", "");
    if(splashDialog.isShowing())
        splashDialog.dismiss(); 
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM