2012-07-12 21 views
1

可能重複:
Android Activity Life Cycle開機畫面有了ProgessBar

我能創建使用的主題我的應用程序的簡單閃屏和它工作得很好。但是,有時候應用程序需要刷新大量數據,我想向用戶顯示一個ProgressBar,讓他們知道在等待的過程中發生了什麼。所以我放棄了主題並創建了一個佈局資源,並將其設置爲我的Splash Activity的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的東西,只是試圖加載與飛濺佈局的活動。它最終顯示出來,但是它首先顯示爲黑屏。無法想象爲什麼這是我的主要啓動器,並且幾乎沒有任何事情發生,只是設置了活動的內容。

並儘可能標題欄顯示出來,我也嘗試添加這對我的清單

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

(我目前已經在其他的活動就好了工作),如果我包括該活動的主題TitleBar消失,但我的佈局資源從不顯示。

回答

1

難道你不知道在整天掙扎後,我發現發佈後很大一部分問題分鐘。簡而言之,我將主題放回到Splash Activity上,這個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)); 
    } 
} 

主要區別是,我只叫StartActivity如果我不跑我的AsyncTask。否則,我從我的AsyncTask的OnPostExecute調用StartActivity。此外,請務必在飛濺佈局中設置所有視圖的背景屬性。由於我在活動中留下了主題,如果您不這樣做,您的主題將顯示爲每個視圖的背景。