0

I plan on checking login status of a user in onCreate of MainActivity - and if the user is logged in - immediately redirect to some other View (e.g. ProfileActivity)

I am worried that MainActivity will flicker into the users view before disappearing. Is this of any concern?

AlanSTACK
  • 5,525
  • 3
  • 40
  • 99
  • 1
    No Activity is not visible during onCreate. It gets visible after onResume() of an acitivty. – mudit_sen May 21 '17 at 06:42
  • I believe not as per http://stackoverflow.com/questions/8515936/android-activity-life-cycle-what-are-all-these-methods-for – MikeT May 21 '17 at 06:43
  • 1
    Just create something like StartupActivity without view and launch any activity you want from there based on conditions you have. – Mirza Brunjadze May 21 '17 at 06:53

3 Answers3

1

You can create an invisible activity as the main activity.

Inside you can place some logic which determines which activity to show to the user first.

Manifest Declaration, with noHistory="true"

<activity
    android:name=".LandingActivity"
    android:launchMode="singleInstance"
    android:noHistory="true" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

LandingActivity class:

public class LandingActivity extends Activity { 

@Override
public void onCreate(Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState); 

    Intent intent = new Intent(); 
    Class<?> firstActivityClass; 
    try{ 
        String className = getFirtsActivityClassName(); 
        firstActivity = Class.forName(className); 
    } 
    catch (ClassNotFoundException e) { 
        firtsActivity = MainActivity.class; 
    } 
    intent.setClass(getApplicationContext(), firstActivity); 
    startActivity(intent); 

    finish(); 
} 

private String getFirtActivityClassName() {       
    // Here the logic 
} 

} 
Nicola De Fiorenze
  • 1,958
  • 1
  • 17
  • 27
  • 1
    This is generally not a good solution as user will spend seconds waiting for activity to launch. If you plan to use this approach at least try the splash pattern as it let user know what is happening and why he has to wait.. – Keivan Esbati May 21 '17 at 07:00
1

I would suggest you to keep MainActivity as LAUNCHER (on singleTask mode) and check the login status before you pass layout to it. This approach will avoid logged in users to wait for 2nd Activity to be launched.

Note that in @Nicola De Fiorenze answer Activity instance will get killed in all cases. Hence you will create 2 instances of Activity even for users who have already passed login phase.

Code may look like this:

Manifest:

<activity
    android:name="MainActivity"
    android:launchMode="singleTask">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

MainActivity:

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    if(!AuthUtils.isLoggedIn()){
        LoginActivity.startActivity(this);
        finish();
        return;
    }

    // Once you know user is logged in, pass layout to activity
    setContentView(resLayoutId);
}
jpact
  • 1,042
  • 10
  • 23
0

It's not Resumed or Started but you can do the action you want.. When Activity is getting created you are allowed to do any action that alter the activity state as it is NOT Stopped or Paused!

For example you can attach a fragment or change a view if you like!

Keivan Esbati
  • 3,376
  • 1
  • 22
  • 36