2013-02-09 59 views
0

我有一個應用程序與共享首選項(第一次)工作,但是,我的第一個要運行的活動是檢查設置首選項。所以當應用程序加載時,你會得到這個「黑屏」,然後加載正確的活動。通過共享首選項加載App啓動

我的應用程序正在擴展一個導航活動,我試圖從這個活動中運行它,但是,我沒有onStart,onCreate()等等,因爲它只是使Actionbar的Sherlock菜單膨脹。如果我添加onStart()我得到一個錯誤。我得到一個無限循環,如果我補充一下:

onStart(){getPrefs();super.onStart();} 

因此,可以跟我的人分享如何/我應該在哪裏運行此,使用戶沒有得到「黑屏」,那麼正確的活動?我只想在應用程序啓動時啓動正確的活動。

下面是我目前加載我的活動:

public class MainActivity extends NavigationActivity { 
    private AQuery aq; 
    SharedPreferences myPrefs; 
    private String lp; 
    private String TAG = "GET PREFS"; 

    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     // show no back arrow 
     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
     setContentView(R.layout.activity_firstload); 

     aq = new AQuery(this); 

     getPrefs(); 

     aq.id(R.id.tv).text("Loading..."); 

    } 

    private void getPrefs() { 
     // Get the xml/preferences.xml preferences 
     SharedPreferences prefs = PreferenceManager 
       .getDefaultSharedPreferences(getBaseContext()); 
     lp = prefs.getString("defaultreport", ""); 
     Log.v(TAG, lp); 

     if (lp.equals("esac")) { 
      Toast.makeText(MainActivity.this, "ESAC", Toast.LENGTH_LONG) 
        .show(); 
      Intent i = new Intent(MainActivity.this, ESACActivity.class); 
      startActivity(i); 
     } else if (lp.equals("sac")) { 
      Toast.makeText(MainActivity.this, "SAC", Toast.LENGTH_LONG) 
        .show(); 
      Intent i = new Intent(MainActivity.this, SACActivity.class); 
      startActivity(i); 
     } else if (lp.equals("msar")) { 
      Toast.makeText(MainActivity.this, "MSAR", Toast.LENGTH_LONG) 
        .show(); 
      Intent i = new Intent(MainActivity.this, MSARActivity.class); 
      startActivity(i); 
     } 

    } 

} 

我NavigationActivity包含:

onCreateOptionsMenu(Menu menu) 

and 

onOptionsItemSelected(MenuItem item) 

編輯::

我曾經想過在啓動時加入了閃屏以適應這種設置變化,但希望看到另一個簡化的解決方案。

編輯編輯::

我試過@MrZander建議,但我遇到了我的清單的問題。

<application 
     android:allowBackup="true" 
     android:icon="@drawable/acricon" 
     android:label="@string/app_name" 
     android:theme="@style/Theme.Sherlock" 
     android:name="com.Conditions.MainActivity" //This loads the PREFS fine 
     > 
     <activity 
      android:name="com.Conditions.SACActivity" //However, defining this negates the PREFS. My apps first screen is defined by the PREFS. 
      android:configChanges="keyboardHidden|orientation|screenSize" 
      android:label="@string/title_activity_main" 
      android:screenOrientation="portrait" 
      android:theme="@style/Theme.Sherlock" 
      android:uiOptions="splitActionBarWhenNarrow" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 

回答

0

我會重寫應用程序的Application

http://developer.android.com/reference/android/app/Application.html

沿着這個東西線:

public class MyApplication extends Application{ 
    @Override 
    public void onCreate() { 
     //Shared prefs here 
    } 
} 

而在你清單的<application>標籤添加該屬性:android:name="com.your.package.MyApplication"

+0

啊!那很棒。我會在這裏嘗試一下。感謝您的評論:) – jasonflaherty 2013-02-11 21:24:58

+0

好吧,所以我試了這個。我的主要活動負載取決於偏好。當我這樣做時,Manifest仍然需要一個.MAIN意圖,這需要從我收集的活動中進行。我用清單更新了我的問題... – jasonflaherty 2013-02-12 04:19:16

+0

我明白了。在這種情況下,啓動屏幕可能是您唯一的選擇。 – MrZander 2013-02-12 07:18:31