-4

The splash activity shows just blank screen. Code is below :

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

public class SplashScreen extends AppCompatActivity {
    private static int SPLASH_TIME_OUT = 3000;
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash_screen);

        try {

            Log.v(" gonna  AsyncTask"," execute AsyncTask");
            new AsyncTask<String, String, String>() {

                @Override
                protected void onPreExecute() {
                    Log.v(" PreExecute AsyncTask"," execute AsyncTask");
                }

                @Override
                protected String doInBackground(String... arg0) {
                    Log.v(" in doInBackground"," execute AsyncTask");
                    try {

                        Log.v(" sleep doInBackground"," execute AsyncTask");
                        Thread.sleep(SPLASH_TIME_OUT);

                    } catch (Exception e) {
                        Log.v(" Exception "," execute AsyncTask");
                        e.printStackTrace();
                    }
                    return null;
                }

                @Override
                protected void onPostExecute(String unused) {

                    Log.v(" onPostExecute "," execute AsyncTask");
                    Intent i = new Intent(SplashScreen.this,
                            MainActivity.class);
                    Log.v(" starting  Activity "," execute AsyncTask");
                    startActivity(i);

                }
            }.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }




    }// end of onCreate

}// end of class SplashScreen

The XML layout file has :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_splash_screen"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.istiaqueahmed.archeology.SplashScreen">

<ImageView

    android:id="@+id/splash_img"
    android:layout_width="170dp"
    android:layout_height="112dp"
    android:adjustViewBounds="true"
    android:scaleType="fitCenter"
    android:layout_centerInParent="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="130dp"
    android:src="@drawable/splash"
    />

<TextView
    android:id="@+id/developed_by"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAlignment="center"
    android:text="@string/developed_by"
    android:layout_below="@id/splash_img"
    android:textStyle="bold"
    android:textSize="13sp"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="30dp"

    />


</RelativeLayout>

Android manifest file has -

 <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        >
        <activity
            android:name=".SplashScreen"
            android:noHistory="true"
            android:screenOrientation="portrait"

            >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

         ........
         </application>

And the style.xml has -

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

Perhaps the activity needs some time for setContentViewto execute and meanwhile the app goes to the MainActivity.

How to prevent the app from showing the blank screen and instead show the splash screen for the intended time period?

Istiaque Ahmed
  • 6,072
  • 24
  • 75
  • 141

3 Answers3

0

Your App is freeze because you are sleeping main Thread.

Thread.sleep(SPLASH_TIME_OUT); // This will sleep your main thread.

Instead of AsyncTask use Handler with delay.

Rahul
  • 10,457
  • 4
  • 35
  • 55
0

Use CountDownTimer instead of AsyncTask.

private static long SPLASH_TIME_OUT = 3000;

new CountDownTimer(SPLASH_TIME_OUT, 1000) {

        public void onTick(long millisUntilFinished)
        {

        }

        public void onFinish()
        {
            Intent i = new Intent(SplashScreen.this,
                        MainActivity.class);
             startActivity(i);
        }
    }.start();
Akash Patel
  • 2,757
  • 1
  • 22
  • 30
  • om first launch after installation, blank screen showed for a while, then the intended splash screen apeared and went to the next activity. Subsequent launces just showed blank screen and went to the next activity. – Istiaque Ahmed Jan 24 '17 at 13:40
0
There is no need to used AsyncTask here.
Please try this code which helps you.

public class WelcomeActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_welcome);
    startHandler();
}

private void startHandler() {
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            Intent i = new Intent(WelcomeActivity.this, MainActivity.class);
            startActivity(i);
            finish();

        }
    };
    Handler handler = new Handler();
    handler.postDelayed(runnable, 3000);
}

}

Chetan Patel
  • 180
  • 8