How to force android to start LoginActivity

3.6k views Asked by At

I want to force my app to run the LoginActivity if there is no account present.

I've done the following but still when I launch the app in the emulator, I have MainActivityTest coming up.

For now I have no Account for my app on the emulator so I expect to simply see the Login screen. However, what I see seems like the MainActivityTest is running

public class AccountAuthenticator extends AbstractAccountAuthenticator {
    private Context context;
    private static final String TAG = "AccountAuthenticator";
    public AccountAuthenticator(final Context context) {
        super(context);
        this.context = context;
    }

    @Override
    public Bundle addAccount(final AccountAuthenticatorResponse response,
                             final String accountType, final String authTokenType,
                             final String[] requiredFeatures, final Bundle options) throws NetworkErrorException {
        final Intent intent = new Intent(context, LoginActivity.class);
        final Bundle bundle = new Bundle();
        bundle.putParcelable(KEY_INTENT, intent);
        return bundle;

    }
  ...
}

public class AccountAuthenticatorService extends Service {

    private static AccountAuthenticator AUTHENTICATOR;

    public IBinder onBind(Intent intent) {
        return intent.getAction().equals(ACTION_AUTHENTICATOR_INTENT) ? getAuthenticator()
                .getIBinder() : null;
    }

    private AccountAuthenticator getAuthenticator() {
        if (AUTHENTICATOR == null)
            AUTHENTICATOR = new AccountAuthenticator(this);
        return AUTHENTICATOR;
    }
}

public class LoginActivity extends RoboSherlockAccountAuthenticatorActivity {
    @Override
    public void startActivity(Intent intent) {
        if (intent != null && ACTION_VIEW.equals(intent.getAction()))
            intent.addCategory(CATEGORY_BROWSABLE);
        super.startActivity(intent);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
    }
}

My AndroidManifest.xml is like below

   <application
          android:icon="@drawable/ic_launcher"
          android:label="myapp" >
       <activity
        android:name=".ui.ui.MainActivityTest"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
       </activity>

    <service
        android:name="com.myapp.AccountAuthenticatorService"
        android:exported="false"
        android:process=":auth" >
        <intent-filter>
            <action android:name="android.accounts.AccountAuthenticator" />
        </intent-filter>

        <meta-data
            android:name="android.accounts.AccountAuthenticator"
            android:resource="@xml/authenticator" />
    </service>
    <activity
        android:name="com.myapp.LoginActivity"
        android:configChanges="orientation|keyboardHidden|screenSize"
        android:excludeFromRecents="true" >
        <!--
            No intent-filter here! This activity is only ever launched by
            someone who explicitly knows the class name
        -->
    </activity>
   </application
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.MANAGE_ACCOUNTS" />
<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_SYNC_SETTINGS" />
<uses-permission android:name="android.permission.READ_SYNC_STATS" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
<uses-permission android:name="android.permission.WRITE_SYNC_SETTINGS" />
2

There are 2 answers

2
Infinite Recursion On

When you launch the app in the emulator, MainActivityTest coming up because your AndroidManifest has:

   <activity
    android:name=".ui.ui.MainActivityTest"
    android:label="@string/app_name" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
   </activity>

If you want to launch LoginActivity by default, use

   <activity
    android:name="com.myapp.LoginActivity"
    android:configChanges="orientation|keyboardHidden|screenSize"
    android:excludeFromRecents="true" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
   </activity>

Else, if you want to display LoginActivity as the first activity based on some conditions, then

  1. In android manifest, keep MainActivityTest as the default activity
  2. In MainActivityTest->onCreate, check the condition and if condition is true, launch LoginActivity
2
Ram kiran Pachigolla On

It would be simple if you put value in shared preference once if you complete login process. and every time when you launch application, once check the sharedpreference value and navigate to activities

 Boolean mobile_register_flag = sharedpref.getBoolean("mobile_register_flag", false);

 if (!mobile_register_flag) {
            Intent intent = new Intent(FlashView.this,
                    RegisterActivity.class);
            startActivity(intent);
        } else {
            Intent intent = new Intent(FlashView.this,
                    ActivityTwo.class);
            startActivity(intent);
        }