1

Possible Duplicate:
Android Activity Life Cycle

I am able to create a simple Splash Screen for my app using Themes and it works just fine. However, there may be times that the app needs to refresh a large amount of data and I want to display a ProgressBar to the user letting them know what's going on while they wait. So I ditched the Theme and created a layout resource and set that as the ContentView for my Splash Activity, but nothing displays. Not only does none of my content display (completely black screen) but the title bar continues to show up despite trying to hide it every way possible.

Splash.axml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:gravity="center">
<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="15dp"
    android:src="@drawable/logo" />
<LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center">
    <ProgressBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="10dp" />
    <TextView
        android:id="@+id/progressText"
        android:text="Loading..."
        android:textSize="20dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5dp" />
</LinearLayout>

Splash.cs

[Activity(MainLauncher=true, NoHistory = true)]
public class Splash : Activity
{
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        RequestWindowFeature(WindowFeatures.NoTitle);

        Window.SetFlags(WindowManagerFlags.Fullscreen, WindowManagerFlags.Fullscreen);

        SetContentView(Resource.Layout.Splash);

        string lastUpdated = PreferenceManager.GetDefaultSharedPreferences(this).GetString("LastInventoryUpdate", null);

        DateTime lastUpdateDate = Convert.ToDateTime(lastUpdated);

        TimeSpan span = DateTime.Today - lastUpdateDate;

        if (string.IsNullOrEmpty(lastUpdated) || span.Days > 30)
        {
            TextView progressText = (TextView)FindViewById(Resource.Id.progressText);
            progressText.Text = "Updating parts database.  May take several minutes...";
            InventoryRepository repository = ((MyApp)Application).Inventory;
            repository.Execute();
        }

        StartActivity(typeof(Login));
    }
}

Still shows logo and title in the title bar and absolutely nothing for content. Even tried taking all the AsyncTask and StartActivity stuff out and just tried loading the Activity with the splash layout. It does eventually show up, but it shows as the black screen for a long while first. Can't imagine why as this is my main launcher and there is literally nothing going on but setting the content of the activity.

And as far as the TitleBar showing up, I've also tried adding this to my manifest (which I currently have working in another activity just fine)

<activity android:name="app.MyApp.Splash" android:theme="@android:style/Theme.NoTitleBar.Fullscreen"></activity>

If I include a Theme for the activity, the TitleBar goes away, but my layout resource never displays.

Community
  • 1
  • 1
jmease
  • 2,507
  • 5
  • 49
  • 89

1 Answers1

1

Wouldn't you know after struggling all day I figured out a large part of the problem minutes after posting. In short, I put the Theme back on the Splash Activity which now shows while everything is initially loading and usually redirects to Login before the content is ever set, which is perfect. But I changed my OnCreate as follows:

[Activity(MainLauncher=true, NoHistory = true,  ScreenOrientation = Android.Content.PM.ScreenOrientation.Landscape, Theme = "@style/Theme.Splash")]
public class Splash : Activity
{
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        RequestWindowFeature(WindowFeatures.NoTitle);

        Window.SetFlags(WindowManagerFlags.Fullscreen, WindowManagerFlags.Fullscreen);

        SetContentView(Resource.Layout.Splash);

        string lastUpdated = PreferenceManager.GetDefaultSharedPreferences(this).GetString("LastInventoryUpdate", null);

        DateTime lastUpdateDate = Convert.ToDateTime(lastUpdated);

        TimeSpan span = DateTime.Today - lastUpdateDate;

        if (string.IsNullOrEmpty(lastUpdated) || span.Days > 30)
        {
            SetTheme(Resource.Drawable.bg_black);
            TextView progressText = (TextView) FindViewById(Resource.Id.progressText);
            progressText.Text = "Updating parts database.  May take several minutes...";
            InventoryRepository repository = ((MyApp) Application).Inventory;
            repository.Execute();
        }
        else
            StartActivity(typeof (Login));
    }
}

Main difference is that I only call StartActivity if I'm not running my AsyncTask. Otherwise, I call StartActivity from OnPostExecute of my AsyncTask. Also, be sure to set the background property of all your views in the splash layout. Since I left the theme on the activity, if you don't do this, your theme will show up as the background for EVERY view.

jmease
  • 2,507
  • 5
  • 49
  • 89