2011-09-08 151 views
3

我希望我的應用只在應用第一次啓動時纔開始活動。 任何人有任何想法?僅啓動一次活動

我發現這個,但它只顯示黑屏。

public class WhatsNew extends Activity { 

public static final String PREFS_NAME = "MyPrefsFile"; 

protected void onCreate(Bundle state){ 

     super.onCreate(state); 
     SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); 
     boolean dialogShown = settings.getBoolean("dialogShown", false); 

     if (!dialogShown) { 
     // AlertDialog code here 

     SharedPreferences.Editor editor = settings.edit(); 
     editor.putBoolean("dialogShown", true); 
     editor.commit();  
     } 
    } 

}

+2

它顯示黑屏,因爲沒有加載沒有佈局(即沒有'setContentView'),所以它顯示一個空的窗口。 – Dave

回答

2

當應用程序啓動,該活動已經運行活動的喜好設置一個標誌。默認您的設置爲false,然後只在標誌未設置時才啓動該活動。請注意,如果用戶清理應用程序數據,或者卸載它並稍後再次安裝,則該活動將再次顯示。

0

取決於「第一次」的含義。最簡單的方法是將一些標誌放入SharedPreferences中......但是可以保留一段時間。 ^^

2

您將需要一個Activity來檢查一個持久布爾值。即,

onCreate(Bundle bundle) 
{ 
    boolean firstRun = // persistance method here 
    Intent toForward; 
    if(firstRun) 
     // Create an intent to start you "only once" activity 
     // persist "firstRun" as false; 
    else 
     // Create an intent for your usual startup 
    startActivity(toForward); 
    finish(); 
} 
0

如果你的意思是應該的onCreate只有一次調用時,「開始只有當應用程序啓動一次」,你可以設置acitivty到singleInstance或singleTask在清單的launchMode。

+0

你確定嗎?文檔中說:「singleTask」和「singleInstance」模式在一個方面也有所不同:「singleTask」活動允許其他活動成爲其任務的一部分,它始終是其任務的根源,但其他活動可以一個「單一實例」活動,另一方面,不允許其他活動成爲其任務的一部分,這是該任務中唯一的活動。如果它啓動了另一個活動,那麼該活動將被分配給另一個任務 - 就像FLAG_ACTIVITY_NEW_TASK的意圖一樣。「 –

3

我認爲你會談論像「登錄」頁面這樣的活動,你只會在應用程序的整個生命週期中啓動一次。 對於這一點,你可以使用sharedPreferences:

SharedPreferences prefs; 
SharedPreferences.Editor editor; 

和你startActivity(意向)下方,添加:

prefs = getSharedPreferences("nbRepet",Context.MODE_PRIVATE); 
editor = prefs.edit(); 
editor.putInt("nbRepet", 1); 
editor.commit(); 

現在你的變量nbRepet有1的值。

之後,你可以添加這些行上方的startActivity(意向)來驗證之前,你的活動從未推出://在這裏恢復nbRepet的價值

..

preferences = MainActivity.this.getSharedPreferences("nbRepet", MODE_PRIVATE);  
int value = preferences.getInt("nbRepet", 0); 

// ..並驗證您的活動是否在之前啓動。

if(value<1) 
{ 
    startActivity(intent); 
} 
0

在這個post看到我的回答,關於同樣的問題。

我建議檢查SharedPreference值是否應用程序以前是否已在onResume()內運行過。

2

這將做的工作適合你

package com.example.startup; 

import android.app.Activity; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.os.Bundle; 
import android.view.View; 

public class MainActivity extends Activity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
    //setContentView(R.layout.activity_main); //we don't need this line 
    SharedPreferences settings=getSharedPreferences("prefs",0); 
    boolean firstRun=settings.getBoolean("firstRun",false); 
    if(firstRun==false)//if running for first time 
     { 
     SharedPreferences.Editor editor=settings.edit(); 
     editor.putBoolean("firstRun",true); 
     editor.commit(); 
     Intent i=new Intent(MainActivity.this,Second.class);//Activity to be  launched For the First time 
     startActivity(i); 
     finish(); 
    } 
    else 
    { 
     Intent a=new Intent(MainActivity.this,Main.class);//Default Activity 
     startActivity(a); 
     finish(); 
    } 
    } 

} 
+0

This Worked for me。Thanks @ JRE.exe – NarenderNishad

相關問題