2012-04-16 158 views
0

當前我正在意圖工作;但是,我遇到了一些問題。關於Android意圖操作

事情是,我有兩個應用程序,A和B.B的午餐時間是android:launchMode =「singleTop」。

現在,我想傳遞一個意圖從A到B,表示「SD卡/ Android」(目錄路徑)。之後,A將完成,B將被創建/恢復/ onNewintent。在第一次,B會收到一個意向字符串「sdcard/Android」,這正是我想要的。

然後我按主頁按鈕到啓動器並再次打開A,然後向B傳遞一個新數據,稱爲「sdcard/Music」,但是出現問題,B不會獲得字符串「sdcard/Music 「,而是B的意圖數據仍然是」SD卡/ Android「。

我期望第二次將數據傳遞給B,onNewintent方法將在B中調用。是否有錯誤?我如何在第二次將正確的數據傳遞給B?

@Override 
public void onCreate(Bundle savedInstanceState) { 
    onNewIntent(getIntent()); 
} 

@Override 
public void onNewIntent(Intent intent) 
{ 
    Log.i("TAG", intent.getStringExtra("path")); 
} 

我知道我應該覆蓋onNewIntent。第一次,B將進入onCreate方法。第二次,我期望它進入onNewIntent方法;然而,它進入了onResumed方法..!

回答

2

方法onNewIntent(...)不是您的第一次爲您的Activity B調用,它只在第二次調用,並且進一步調用Activity B時。

你可以「重新寫」原來Intent通過執行類似下面的...

@Override 
protected void onNewIntent(Intent intent) { 
    setIntent(intent); 
} 

@Override 
protected void onResume() { 
    super.onResume(); 
    handleIntent(getIntent()); 
} 

private void handleIntent(Intent intent) { 
    // The intent parameter here will be the original `Intent` the first 
    // time Activity B is started. It will be the new Intent after that 
    // as onNewIntent(...) re-writes it with the call to setIntent(...) 
} 
+0

我把東西在我的文章,謝謝。 – s011208 2012-04-16 07:02:59

+0

問題是,B從來沒有進入onNewIntent方法!爲什麼? – s011208 2012-04-16 07:04:37

+0

不要直接調用'onNewIntent(...)'並且不要使用'getIntent()'將它傳遞給現有的'Intent' - 這正是你不想做的。 'onNewIntent(...)'方法是由Android框架自動調用的,你不應該自己調用它。 – Squonk 2012-04-16 08:12:05