21

I want to know how to achieve this effect.

My app (default everything), when launcher icon is clicked, right away display some kind of a empty dummy window, where nothing is happening and then loads layout into it.

"Heavier" apps like YouTube, Drive, Dropbox etc. when started, seem to wait after launch, without showing that dummy window and load right into ready layout.

Any idea how to do this or where should I look into?

Thanks

//EDIT: this has nothing to do with something like loading database, where I should display progressBar, imho this has to do with stuff before activity exists.

urSus
  • 12,492
  • 12
  • 69
  • 89

5 Answers5

32

I suggest you don't disable the dummy loading window using:

<style name="Theme.MyTheme" parent="android:style/Theme" >
    ....
    <item name="android:windowDisablePreview">true</item>
    ....
</style>

because you could run into several problems. For example, if you try to animate an AlertDialog, enter animation will not work (personal experience).

The right solution is to set the theme of your application like to your main activity screen (same ActionBar style, same background color). In my application after severals experiments i found the right solution for my needs. (I have a main activity with blank background, no ActionBar, just full screen personal layout)

1 - styles.xml: add new style like this (remove ActionBar and set the same background color of my main activity)

<style name="LoadingTheme" parent="@android:style/Theme.Light.NoTitleBar.Fullscreen">
    <item name="android:windowBackground">@color/white_smoke</item>
</style>

2 - AndroidManifest.xml: set my theme for main activity to "LoadingTheme"

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
        <activity
            android:name="com.company.appname.MainActivity"
            android:label="@string/app_name"
            android:theme="@style/LoadingTheme">
        <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
    </activity>
    ....
</application>

And finally i have full blank dummy loading window without ActionBar with soft MainActivity load and working animations.

Hope to be helpful.

JDCoder
  • 541
  • 6
  • 9
  • @JDCoder I agree with you to not using android:windowDisablePreview=true but I have a problem with it. I have a splash screen in the blue background that it can be changed to orange as theme preference by the user. When it is blue then changed to the orange theme when the app is launching instead showing orange in preview window it first showed blue for a moment then changed to orange style. How can I resolve this issue? – Mohammad Afrashteh Jun 06 '18 at 04:00
  • @MohammadAfrashteh you foud a solution regarding this? – Moustafa EL-Saghier Jan 22 '22 at 23:50
10
<style name="Theme.MyTheme" parent="android:style/Theme" >
    ....
    <item name="android:windowDisablePreview">true</item>
    ....
</style>

but use with caution

urSus
  • 12,492
  • 12
  • 69
  • 89
  • However, this approach can result in a longer startup time than apps that don’t suppress the preview window. Also, it forces the user to wait with no feedback while the activity launches, making them wonder if the app is functioning properly. Refer this link https://developer.android.com/topic/performance/vitals/launch-time – Dong Thang Apr 11 '19 at 08:18
2

This will create a custom preview window for your activity.

Create a style in styles.xml like

<style name="MyTheme" parent="@android:style/Theme.Holo.NoActionBar">
    <item name="android:windowBackground">@color/myBackgroundColor</item>
</style>

In your Manifest file use the theme created above. For eg:

<activity android:name=".MyActivity"
   android:label="@string/app_name"
   android:theme="@style/MyTheme"
   />

In your activity change the background of the window back to null in onCreate()

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().setBackgroundDrawable(null);
    setContentView(R.layout.main);
}
Tarun
  • 13,727
  • 8
  • 42
  • 57
  • only this solution worked for me. However I've applied drawable - with single `item : drawable = @ color` instead of `null` – murt Dec 22 '20 at 15:28
0

You can call the setContentView method anywhere you'd like in your activity. Before calling this method it will display a blank screen. If you wanted a splash screen, you can call the setContentView method within the onCreate method to display the splash screen view. Then when you're ready to display the "main" view you can use a LayoutInflater to display it. I also suggest using a ProgressDialog to show the user that information is loading.

chRyNaN
  • 3,592
  • 5
  • 46
  • 74
  • The problem is, my app has a lot of static resources etc, so my guess is those are loaded before the activity exists, so thats why that blank window. Then in activity I have cursor loader and I am displaying ProgressBar, that just blinks through as database is not that big. .. What I want to avoid is that first dummy window at all, no splash screen, as those mentioned apps do. – urSus Jun 04 '13 at 16:58
0

Load your resources before setting the contentview. There will be a dummy window until you would not set view using setContentView.

EDIT To avoid this screen. Make there changes to your code.

1) Do not do any heavy work in activity onCreate method. Run a separate thread for it.

2) Set content view right after the super.onCreate() line

   super.onCreate(savedInstanceState);
   setContentView (R.layout.splash_screen);

3) Apply following theme to your application.

Define theme in style

<resources>
<style name="Theme.MyTheme" parent="android:style/Theme" >
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowBackground">@drawable/any_default_background</item>

</style>
</resources>

In your Manifest

      <application
        ....

        android:theme="@style/Theme.MyTheme"
        ....
         > 

Thats It.

Adnan
  • 5,025
  • 5
  • 25
  • 44
  • That kinda doesnt make sense to me. I should load resources before setContentView() and the dummy window will be displayed until setContentView is not set, right? I dont NOT want that dummy window. – urSus Jun 04 '13 at 16:51
  • Then it is not a good app behavior to let the user wait for application without showing any starting screen. You should display some splash screen while your resources are being load in background. – Adnan Jun 04 '13 at 16:56
  • Its just second or two, but that blank window gives out bad UX in my opionion, so I want to skip it. Google apps do it too (Gmail displays the dummy window, Drive doesnt - check it out) / When I am loading the database ofcourse I am showing the progress bar, but in my opinion, I am waiting for all the static resources etc, not the activity. – urSus Jun 04 '13 at 17:01
  • You are right. I just notice that. I am not sure but changing activity launching style may be effective. – Adnan Jun 04 '13 at 17:11
  • yea, Google Goggles and Evernote does it too, I am not making it up :-D – urSus Jun 04 '13 at 17:18
  • Viasto, try once using this Theme. Hope it will work for you. :) – Adnan Jun 04 '13 at 18:16
  • ..actually its android:windowDisablePreview – urSus Jun 06 '13 at 09:39