Skip to content

Instantly share code, notes, and snippets.

@muthuraj57
Last active January 7, 2020 02:07
Show Gist options
  • Save muthuraj57/57823550f30ea5b55926b28be361e986 to your computer and use it in GitHub Desktop.
Save muthuraj57/57823550f30ea5b55926b28be361e986 to your computer and use it in GitHub Desktop.
public class SplashActivity extends AppCompatActivity {
private static final int REQ_CODE = 23;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
setTheme(R.style.AppTheme);
super.onCreate(savedInstanceState);
if (shouldFinishActivity()) {
return;
}
setContentView(R.layout.activity_splash);
onActivityOpened();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQ_CODE) {
finish();
}
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
if (shouldFinishActivity()) {
return;
}
onActivityOpened();
}
/*
* http://stackoverflow.com/a/23220151/3423932
*
* with launchMode singleTask | singleInstance, pressing home from inner activity and opening app through launcher icon
* always opens launcher activity instead of resuming old activity. As a workaround, we finish the
* activity if it is not root activity
* */
private boolean shouldFinishActivity() {
if (ActivityLifeCycleCallback.Companion.getActivityCount() > 1) {
finish();
return true;
}
if (!isTaskRoot()
&& getIntent().hasCategory(Intent.CATEGORY_LAUNCHER)
&& getIntent().getAction() != null
&& getIntent().getAction().equals(Intent.ACTION_MAIN)) {
finish();
return true;
}
return false;
}
private void onActivityOpened() {
Class<? extends AppCompatActivity> activityClass;
if (PreferencesUtil.INSTANCE.isOnBoardingShownAlready()) {
activityClass = MainActivity.class;
} else {
activityClass = OnBoardingActivity.class;
}
Intent intent = new Intent(getIntent().getAction(), getIntent().getData(), this, activityClass);
startActivityForResult(intent, REQ_CODE);
overridePendingTransition(0, 0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment