2010-04-14 58 views
0

我試圖從onStart啓動一個浮動活動,以便在初始活動開始時從用戶權限中檢索一些信息。我有以下幾點:在onStart中啓動活動的問題

@Override 
public void onStart(){ 
super.onStart(); 
callProfileDialog(); 
} 

而且callProfileDialog()就是:

private void callProfileDialog(){ 
Intent i = new Intent(this, com.utility.ProfileDialog.class); 
    startActivityForResult(i, PROFDIALOG); 
} 

ProfileDialog.class返回從一個輸入框的字符串。如果返回的結果是RESULT_CANCELED,那麼我重新開始活動。

我遇到的問題是,當程序啓動時,屏幕只是黑色。如果我點擊返回按鈕RESULT_CANCELED返回,那麼最初的活動顯示以及浮動活動(因爲它得到RESULT_CANCELED時它自己召回)。爲什麼我無法通過從onStart()調用ProfileDialog.class來獲得活動顯示?當我在onCreate()的末尾調用它時,我得到了相同的結果,這是我切換到使用onStart()的方式。謝謝您的幫助。

編輯:我也曾嘗試以下操作:

@Override 
public void onWindowFocusChanged(boolean hasFocus){ 
    if(hasFocus) 
    callProfileDialog(); 
} 

但是,這也不行。一旦我點擊後退按鈕,一切正常,但沒有這樣做,它全部是黑色的。

回答

0

我認爲這是因爲你還沒有有效的背景。嘗試下列操作之一:

@Override 
public void onCreate(Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState); 
    callProfileDialog(); 
} 

@Override 
public void onResume(){ 
    super.onResume(); 
    callProfileDialog(); 
} 
+0

第一個建議有和我目前一樣的問題。第二個建議雖然不僅僅是黑色,但一旦我點擊後退按鈕,它也不斷啓動浮動活動。 – Fizz 2010-04-14 20:13:51

0

您應該重寫Activity.onActivityResult(),並設置你是從一個孩子返回一個標誌,只有推出新的活動,如果該標誌是不正確的:

public class MyActivity extends Activity { 
    boolean returningFromChild = false; 
    @Override 
    public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    returningFromChild = true; 
    } 

    @Override 
    protected void onCreate(Bundle icicle) { 
    super.onCreate(icicle); 
    // Use your main layout here 
    setContentView(R.layout.main); 
    } 

    @Override 
    protected void onResume() { 
    super.onResume(); 
    if (!returningFromChild) { 
     callProfileDialog(); 
    } 
    returningFromChild = false; 
    } 
} 

// ProfileDialog.java 
public class ProfileDialog extends Activity { 
    @Override 
    protected void onCreate(Bundle icicle) { 
    super.onCreate(icicle); 
    // Use your dialog layout here 
    setContentView(R.layout.profile_dialog); 

    // Use the id of your "OK" button here: 
    Button btn = (Button) findViewById(R.id.btnSaveInput); 

    btn.setOnClickListener(new View.OnClickListener { 
     public void onClick(View v) { 
     // XXX: Get/validate the user's input. Can add it to a new Intent object as an Extra, 
     // and use the setResult(RESULT_OK, intent); version: 
     setResult(RESULT_OK); 
     finish(); 
     } 
    }); 
    } 
} 
+0

我得到同樣的問題。當我啓動應用程序時,它掛在黑屏上。我仍然需要點擊後退按鈕才能返回RESULT_CANCELED以顯示任何內容。 – Fizz 2010-04-17 18:07:10

+0

兩個活動(MainActivity和ProfileDialog)都調用'setContentView(R.layout.layout_resource_file);'?我一定是讀了你最初的問題,我認爲唯一的問題是它不斷重新啓動對話活動。 如果您剛剛獲得黑屏,通常意味着您沒有該活動中的任何內容(並且默認主題爲.Dark)。 至於RESULT_CANCELED,你是否在你從用戶檢索到你想要的輸入後調用ProfileDialog中的'setResult(RESULT_OK,data);'? ProfileDialog中有一個按鈕,它會調用'finish()'? – Joe 2010-04-20 21:09:02

+0

我已經用主流活動和ProfileDialog活動的更明確流程編輯了我的答案,以顯示取回RESULT_OK所需的內容。 – Joe 2010-04-20 21:42:03

2

我有一個類似的問題,並通過覆蓋onAttachedToWindow()來獲得我想要的行爲。

@Override 
public void onAttachedToWindow() { 
    super.onAttachedToWindow(); 
    callProfileDialog(); 
} 
+0

謝謝,我會試試這個。 – Fizz 2010-06-12 01:17:57