3

when I start my Activity, I notice a short delay. I click on my app icon and the home screen stays on the screen for about 1-1.5 sec before my activity is shown.

My activity's onCreate method takes about 800ms to finish.

I've also noticed that setting android:screenOrientation="landscape" also adds a noticeable delay, even when I use a test Activity with an empty layout.

Is there a way to get rid of this delay, or at least show a black screen while the UI is loading?

Edited: see code for test activity below. In my real activity there is a bunch of other loading including GUI elements and engine logic, sound, etc. The real issue is that the delay is visible even using this small test activity.


Code for test activity:

public class TestActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Set full screen mode and disable
        // the keyguard (lockscreen)
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN  
                             | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD 
                             | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                             | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

        setContentView(R.layout.main);
    }
}

Layout XML:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:keepScreenOn="true"  >
</FrameLayout>

Manifest:

<activity
    android:name=".TestActivity"
    android:configChanges="orientation|keyboardHidden"
    android:label="@string/app_name"
    android:screenOrientation="landscape"
    android:windowSoftInputMode="stateAlwaysHidden|adjustPan" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
hthms
  • 853
  • 1
  • 10
  • 25

2 Answers2

2

you could set the default activity to a near blank activity that does nothing more than show a background and start your real activity.. like a splash screen

Drake Clarris
  • 1,047
  • 6
  • 10
  • Ok. How do I start the main activity from this splash? If I put the activity launch into the onCreate / onResume / ... then the problem still appear. And where am I suppose to finish() the splash activity? – hthms May 01 '12 at 19:26
  • with something like this in your onCreate() method for the 'blank' main activity - startActivity( new Intent( getApplicationContext(), RealActivity.class ) ); – Drake Clarris May 01 '12 at 19:45
  • Thanks. I also had to add `android:noHistory="true"` and `android:theme="@android:style/Theme.NoTitleBar.Fullscreen"` for a smooth transition between the activities. – hthms May 01 '12 at 20:28
0

The Ans Drake gave is perfect but to extend it - let you want to have your custom splash screen image visible for the whole time, If we apply color to the window then there will be a switch from color to the actual slash screen. Which could be a bad UX. The ans which i propose dont require setContentView but only manage splash screen through theme.

We can't set the theme in java as in my case the control came to onCreate of my splash activity very late. Till that time black screen keep visible.

This gave me idea that window has to be managed from theme which we specify in menifest.

There is the menifest i have:

<activity
        android:name=".main.activities.SplashScreen"
        android:theme="@style/Splash"
        android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
</activity>

Now The theme i created is as follows:

<style name="Splash" parent="@style/Theme.AppCompat.Light">
    <item name="android:windowBackground">@drawable/splash</item>
    <item name="android:windowNoTitle">true</item>
    <item name="windowNoTitle">true</item>
    <item name="colorPrimaryDark">@color/green_09</item>
    <item name="colorPrimary">@color/green_09</item>
    <item name="windowActionBar">false</item>
</style>

Splash in the drawable resource which contains a bitmap resource, I have to tweak a bit to make it look perfect not stretched and in center:

<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:antialias="true"
    android:dither="true"
    android:gravity="fill"
    android:src="@drawable/splash_screen" />
Gem
  • 1,516
  • 16
  • 21