1

重複の可能性:
Androidアクティビティのライフサイクル

テーマを使用してアプリ用のシンプルなスプラッシュ画面を作成できますが、問題なく動作します。ただし、アプリで大量のデータを更新する必要がある場合があります。ユーザーにプログレスバーを表示して、待機中に何が起こっているかを知らせたいと思います。そこで、テーマを破棄してレイアウトリソースを作成し、それをスプラッシュアクティビティのContentViewとして設定しましたが、何も表示されません。コンテンツが表示されない(完全に黒い画面)だけでなく、タイトルバーは、可能な限り非表示にしようとしても表示され続けます。

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));
    }
}

それでもタイトルバーにロゴとタイトルが表示され、コンテンツにはまったく表示されません。すべてのAsyncTaskとStartActivityのものを取り出して、スプラッシュレイアウトでアクティビティをロードしてみました。最終的には表示されますが、最初は長い間黒い画面として表示されます。これが私のメインランチャーであり、文字通り何も起こっていないので、なぜアクティビティのコンテンツを設定するのか想像できません。

また、TitleBarが表示される限り、これをマニフェストに追加してみました(現在、別のアクティビティで問題なく作業しています)。

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

アクティビティのテーマを含めると、TitleBarは消えますが、レイアウトリソースが表示されません。

4

1 に答える 1

1

一日中苦労した後、投稿して数分後に問題の大部分を理解したことをご存知でしょうか。つまり、テーマをスプラッシュアクティビティに戻します。これは、すべてが最初に読み込まれている間に表示され、通常、コンテンツが設定される前にログインにリダイレクトされます。これは完璧です。しかし、OnCreateを次のように変更しました。

[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));
    }
}

主な違いは、AsyncTaskを実行していない場合にのみStartActivityを呼び出すことです。それ以外の場合は、AsyncTaskのOnPostExecuteからStartActivityを呼び出します。また、スプラッシュレイアウトでは、すべてのビューのbackgroundプロパティを必ず設定してください。アクティビティにテーマを残したので、これを行わないと、すべてのビューの背景としてテーマが表示されます。

于 2012-07-12T22:02:08.667 に答える