Currently we have a splash screen that is displayed in our app. However, if there is no data to be gathered or processed that is waiting, we'd like to go straight into our first activity. Is there a way to do that without having the splash screen flash?
The AndroidManifest.XML of the splashscreen portion is as follows:
<activity android:name="com.example.SplashScreenActivity"
android:label="@string/app_name"
android:noHistory="true" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
What I have done successfully in the past is to create an invisible activity as the main activity. It never gets shown to the user, because it launches the "correct" activity in the constructor.
For this reason, there is no need to theme the activity as "invisible" as it does not load a view.
Inside I place some logic which determines which activity to show to the user first. This works perfectly for my use case - give it a try.
Manifest Declaration (note the noHistory="true"
parameter):
<activity
android:name=".activity.EntryActivity"
android:noHistory="true" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
EntryActivity class:
public class EntryActivity extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// launch a different activity
Intent launchIntent = new Intent();
Class<?> launchActivity;
try
{
String className = getScreenClassName();
launchActivity = Class.forName(className);
}
catch (ClassNotFoundException e)
{
launchActivity = DefaultHomeActivity.class;
}
launchIntent.setClass(getApplicationContext(), launchActivity);
startActivity(launchIntent);
finish();
}
/** return Class name of Activity to show **/
private String getScreenClassName()
{
// NOTE - Place logic here to determine which screen to show next
// Default is used in this demo code
String activity = DefaultHomeActivity.class.getName();
return activity;
}
}
Given that the launcher will start the activity you specify in your Manifest, it's not possible to set conditions on whether that activity will be started (or another).
So you're left with the options as Richard Le Mesurier and dors suggest:
I'd prefer the second option, or if you're planning to introduce Fragments anyway, use them here:
Fragments
. Start your main activity, which has a Fragment placeholder. If you need to show Splash Screen, load the SplashScreenFragment in that activity, otherwise, load the Fragment that constitutes the first useful screen to the user.As an aside, the use of splash screens is discouraged; as a user, I'd prefer to see the main Activity with most of the static UI components loaded immediately, and some on-screen indication that something is loading/updating.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With