1

可能重复:
Android Activity 生命周期

我可以使用主题为我的应用程序创建一个简单的启动画面,并且效果很好。但是,有时应用程序可能需要刷新大量数据,我想向用户显示一个 ProgressBar,让他们知道他们在等待时发生了什么。所以我放弃了 Theme 并创建了一个布局资源并将其设置为我的 Splash Activity 的 ContentView,但没有显示任何内容。我的内容不仅没有显示(完全黑屏),而且标题栏继续显示,尽管试图以各种可能的方式隐藏它。

飞溅.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>

飞溅.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 的东西,并尝试使用启动布局加载 Activity。它最终确实出现了,但它首先显示为黑屏很长一段时间。无法想象为什么这是我的主要启动器,实际上除了设置活动内容之外什么都没有。

至于出现的 TitleBar,我也尝试将它添加到我的清单中(我目前在另一个活动中工作得很好)

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

如果我为活动包含一个主题,标题栏就会消失,但我的布局资源永远不会显示。

4

1 回答 1

1

你不知道在挣扎了一整天后,我在发布后几分钟就发现了很大一部分问题。简而言之,我将主题放回了 Splash Activity,它现在在所有内容最初加载时显示,并且通常在设置内容之前重定向到 Login,这是完美的。但我改变了我的 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。此外,请务必在启动布局中设置所有视图的背景属性。由于我将主题留在活动中,如果您不这样做,您的主题将显示为每个视图的背景。

于 2012-07-12T22:02:08.667 回答