Fritz Fritz - 1 month ago 14
Android Question

MonoDroid Splash Screen

How can I implement a simple "splash screen" on program startup? I am copying a SQLite DB and it can be a bit of a long process that is not UI "friendly" .

I would prefer not to use "java code".

TIA

Answer

I recently solved this problem in the following way.

In the main activity I passed a parameter via the intent to set the number of milliseconds for which the splash screen would remain visible.

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.Main);

        Intent i=new Intent();
        i.SetClass(this, typeof (Splash));
        i.PutExtra("Milliseconds", 3000);
        StartActivity(i);
    }

Then, in the second activity which I named "Splash" I retrieved the value and set a second thread to end the activity when the time had elapsed.

[Activity(Label = "Daraize Tech")]
public class Splash : Activity
{
    private int _milliseconds;
    private DateTime _dt;

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        _milliseconds = Intent.GetIntExtra("Milliseconds", 1000);
        SetContentView(Resource.Layout.Splash);
        _dt=DateTime.Now.AddMilliseconds(_milliseconds);
     }

    public override void OnAttachedToWindow()
    {
        base.OnAttachedToWindow();

        new Thread(new ThreadStart(() =>
                                    {
                                    while (DateTime.Now < _dt)
                                        Thread.Sleep(10);
                                    RunOnUiThread( Finish );                                                   
                                    }
            )).Start();
    }

}