Reputation: 1
I have 2 activities, LoginActivity
and MainActivity
. In the first activity i'm logging in the user and redirect him to the MainActivity
. I have also have created in the MainActivity
a sign-out button which calls this sign-out method:
private void signOut() {
Auth.GoogleSignInApi.signOut(googleApiClient).setResultCallback(new ResultCallback<Status>() {
@Override
public void onResult(Status status) {
Intent intent = new Intent(MainActivity.this, LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
});
}
Every time the app starts, i check if the user in logged in. If it's logged in, the user remains in the MainActivity
and if not, it is redirected to the LoginActivity
.
The problem is, when the user is not logeed in, the app first of all starts the MainActivity
and after that it starts the LoginActivity
.
This is how my AndroidManifest
looks like:
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".LoginActivity" />
How can i start the app, when the user is not logged in, without opening the MainActivity
?
Upvotes: 1
Views: 3240
Reputation: 1546
I think at best what you can do is have two layouts in MainActivity
, say MainLayout
and LoginLayout
, hide the MainLayout
if the user is not logged in, and show LoginLayout
in the same Activity, i.e. MainActivity
, and make the user login, then again hide the LoginLayout
and show the MainLayout
. Or, rather than hiding the LoginLayout
, restart the Activity.
Put these conditions at the start of onCreate()
under if-else
statements.
Somewhat like this (in your onCreate
of MainActivity
):
//Initially, have your LoginLayout GONE and MainLayout VISIBLE
if (!isUserLoggedIn) {
MainLayout.setVisibility(View.GONE);
LoginLayout.setVisibility(View.VISIBLE);
/*
Code to get user logged in,
then restart activity,
or simply hide LoginLayout and show MainLayout
*/
}
Upvotes: 2
Reputation: 36045
You create a third Activity which is your true launch activity. Then just this determines which should be the next one.
@Override
public void onCreate(Bundle si) {
...
if (isLoggedIn()) {
goToMain();
} else {
goToLogin();
}
finish();
}
This will open up an Activity which decides where to go after. You may see a slight flash on the screen when the Activity loads. Here's a discussion on styling a transparent activity. `
Upvotes: 2
Reputation: 440
Move the decision making code to a third activity and set it as the new launch activity.
if (condition1) {
startActivity(intent1);
}
else {
startActivity(intent2);
}
And change the theme of the new activity as below
android:theme="@android:style/Theme.NoDisplay
Upvotes: 1