softeyes
softeyes

Reputation: 47

Android Splash screen

I have implemented splash screen in 2 different ways using activity other than main activity and using dialog in main activity.

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). Now activity_main layout has a listview with string array entries. 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;
    }

}

Upvotes: 0

Views: 1125

Answers (1)

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132992

probably onPostExecute method not called so just put @Override before onPostExecute to make sure you are Overriding AsyncTask onPostExecute method as :

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

Upvotes: 3

Related Questions