Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Splash screen show blank white screen when performing AsyncTask

I have an app that rates fund exchanges and shows them in a RecyclerView.
My app was working just fine before I made a simple change to the theme. I changed it from @android:style/Theme.AppCompat.Light.NoActionBar"
to
@android:style/Theme.NoTItleBar.Fullscreen
because I wanted to hide the Status Bar when my app is loading on startup.


Manifest

        <!-- Splash Screen -->
    <activity android:name=".Splash"
        android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>

Splash class

public class Splash extends Activity {
private final int SPLASH_DISPLAY_LENGHT = 5000;

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

    new InitialViewLoad().execute();
}

private class InitialViewLoad extends AsyncTask<Void, Void, Long> {
    @Override
    protected Long doInBackground(Void ... params) {
        long before = System.currentTimeMillis();
        List<Fund> funds = FundPortfolio.get(getApplicationContext()).getFunds();
        new PricesFetcher().fetchItems(funds);
        for (Fund fund : funds) {
            new FinanceFetcher().fetchItems(fund);
            FundPortfolio.get(getApplicationContext()).updateFund(fund);
        }
        long after = System.currentTimeMillis();
        long time = after - before;
        return time;
    }

    @Override
    protected void onPostExecute(Long time) {
        Intent i = new Intent(Splash.this, MainActivity.class);
        synchronized (this) {
            try {
                if (SPLASH_DISPLAY_LENGHT - time > 0) {
                    wait(SPLASH_DISPLAY_LENGHT - time);


         }
                } catch (InterruptedException ioe) {
                    Log.e("Blah,Blah,Blah", "Blah", ioe);
                } finally {
                    startActivity(i);
                    finish();
                }
            }
        }
    }
}

If I don't run the InitialViewLoad().execute(); the Splash Screen shows as normal.
If I have a fund added in my main activity the Splash Screen works as it did before the change.
If the RecyclerView is empty (doesn't contain any funds) then a blank white screen shows instead of the Splash screen.

I cannot for the life of me figure out what is causing this.
Sorry if the explanation is bad. If you have any questions about any code you need to see to solve this I'll be happy to provide that.
Thanks!


Update ##

initial_activity.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:background="#1b5e20"
              android:gravity="center"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <ImageView
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:background="@drawable/logo"
        android:layout_gravity="center"/>

</LinearLayout>

Update 2

Fixed the problem. I used a Timertask and that worked

protected void onPostExecute(Long time) {
    final Intent i = new Intent(Splash.this, MainActivity.class);
    handler = new Handler();

    timer = new Timer();
    TimerTask timerTask = new TimerTask() {
        @Override
        public void run() {
            handler.post(new Runnable() {
                @Override
                public void run() {
                    startActivity(i);
                    finish();
                }
            });
        }
    };
    if (SPLASH_DISPLAY_LENGHT - time > 0)
        timer.schedule(timerTask, SPLASH_DISPLAY_LENGHT - time);
    else
        timer.schedule(timerTask, 5000);
}
like image 330
speccaN Avatar asked Oct 31 '22 03:10

speccaN


1 Answers

This solution worked for me

In order to open the app fast we need to disable the instant run... this happens for API 23 (Marshmallow) and above

Follow these steps for the same :

file -> settings -> build, execution, deployment -> Instant Run disable all four options

like image 125
Mohammad Khan Avatar answered Nov 09 '22 17:11

Mohammad Khan