Skip to content

Instantly share code, notes, and snippets.

@chanakin
Created December 22, 2016 19:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save chanakin/c44bf1c6a9a80d2640440b5aaa92c8ee to your computer and use it in GitHub Desktop.
Save chanakin/c44bf1c6a9a80d2640440b5aaa92c8ee to your computer and use it in GitHub Desktop.
Android Handle Logged In/Out State on Startup
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.android">
...
<application
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name">
<activity
android:name=".onboarding.StartupActivity"
android:label="@string/app_name"
android:launchMode="singleInstance"
android:theme="android:style/Theme.NoDisplay">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:launchMode="singleTop" />
<activity
android:name=".authentication.controller.AuthenticationActivity"
android:label="@string/title_sign_in"
android:launchMode="singleTask"
android:windowSoftInputMode="adjustResize|stateHidden" />
....
</application>
</manifest>
package com.example.android.onboarding;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import com.example.android.MainActivity;
import com.example.android.R;
import com.example.android.authentication.controller.AuthenticationActivity;
import com.example.android.util.ResourceUtils;
public class StartupActivity extends Activity {
private static final AUTHENTICATION_REQUEST_CODE = 1000;
@Override
protected void onCreate(Bundle savedInstanceState) {
if (isLoggedIn()) {
Intent startupIntent = new Intent(this, MainActivity.class);
startupIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(startupIntent);
finish();
} else {
Intent startupIntent = new Intent(this, AuthenticationActivity.class);
startActivityForResult(startupIntent, AUTHENTICATION_REQUEST_CODE);
}
super.onCreate(savedInstanceState);
}
private boolean isLoggedIn() {
// Check SharedPreferences or wherever you store login information
return this.getSharedPreferences("my_app_preferences", Context.MODE_PRIVATE).getBoolean("loggedIn", false);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == AUTHENTICATION_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
Intent startupIntent = new Intent(this, MainActivity.class);
startupIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(startupIntent);
}
finish();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment