Skip to content

Commit

Permalink
Update splash screen loader [ci skip]
Browse files Browse the repository at this point in the history
  • Loading branch information
danimahardhika committed Dec 1, 2017
1 parent 04a1b1c commit e5d204a
Showing 1 changed file with 27 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.danimahardhika.android.helpers.core.utils.LogUtil;

import java.lang.ref.WeakReference;
import java.util.concurrent.Executor;

import uk.co.chrisjenx.calligraphy.CalligraphyContextWrapper;

Expand Down Expand Up @@ -54,7 +55,9 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
mConfig = onInit();
initBottomText();

mAsyncTask = new SplashScreenLoader(this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
mAsyncTask = SplashScreenLoader.with(this)
.mainActivity(mConfig.getMainActivity())
.start(AsyncTask.THREAD_POOL_EXECUTOR);
}

@Override
Expand All @@ -66,6 +69,7 @@ protected void attachBaseContext(Context newBase) {
protected void onDestroy() {
if (mAsyncTask != null) {
mAsyncTask.cancel(true);
mAsyncTask = null;
}
super.onDestroy();
}
Expand All @@ -87,14 +91,28 @@ private void initBottomText() {
}
}

private class SplashScreenLoader extends AsyncTask<Void, Void, Boolean> {
private static class SplashScreenLoader extends AsyncTask<Void, Void, Boolean> {

private WeakReference<Context> context;
private Class<?> mainActivity;

private SplashScreenLoader(@NonNull Context context) {
this.context = new WeakReference<>(context);
}

private SplashScreenLoader mainActivity(@NonNull Class<?> mainActivity) {
this.mainActivity = mainActivity;
return this;
}

private AsyncTask start(@NonNull Executor executor) {
return executeOnExecutor(executor);
}

private static SplashScreenLoader with(@NonNull Context context) {
return new SplashScreenLoader(context);
}

@Override
protected Boolean doInBackground(Void... voids) {
while (!isCancelled()) {
Expand All @@ -117,15 +135,17 @@ protected void onPostExecute(Boolean aBoolean) {
if (((Activity) context.get()).isFinishing()) return;
}

mAsyncTask = null;
if (aBoolean) {
Intent intent = new Intent(context.get(), mConfig.getMainActivity());
Intent intent = new Intent(context.get(), mainActivity);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

startActivity(intent);
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
finish();
if (context.get() instanceof Activity) {
Activity activity = (Activity) context.get();
activity.startActivity(intent);
activity.overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
activity.finish();
}
}
}
}
Expand Down

0 comments on commit e5d204a

Please sign in to comment.