2013-11-03 35 views
0

我已將我的應用程序與txt文件關聯。 因此用戶可以在文件資源管理器應用程序中選擇一個文件並選擇我的應用程序來打開它。 在OnCreate中:android現有活動從OnResume讀取文件的意圖

Intent intent = getIntent(); 
    String action = intent.getAction();   
    if (action != null && action.equals(Intent.ACTION_VIEW)) { 
     txtFilePath = intent.getData().getPath(); 
    } 

然而,當用戶沒有關閉我的應用程序,他又回到資源管理器,打開另一個文件,在OnCreate不叫。 OnResusme被調用。 我把下面的代碼中的onResume:

Intent intent = getIntent(); 
    String action = intent.getAction();   
    if (action != null && action.equals(Intent.MAIN)) { 
     txtFilePath = intent.getData().getPath(); 
    } 

然而,txtFilePath爲空。爲什麼?非常感謝您的幫助。

+0

使用** txtFilePath /意圖**變量,全局和使用,如果沒有重新分配中的onResume ** txtFilePath /意圖** ()。 – Ajeesh

+0

發佈您的清單 –

回答

0

不要在onResume()方法中創建Intent的新對象而應使用您在onCreate方法中使用的現有對象。

 Intent intent; 

的onCreate:

intent = getIntent(); 
    String action = intent.getAction();   
    if (action != null && action.equals(Intent.ACTION_VIEW)) { 
     txtFilePath = intent.getData().getPath(); 
    } 

的onResume:

String action = intent.getAction();   
if (action != null && action.equals(Intent.MAIN)) { 
    txtFilePath = intent.getData().getPath(); 
} 
+0

我試過了你的方法。我發現OnResume中的「String action = intent.getAction();」沒有得到更新。 Intent.ACTION_VIEW「和InCreate中第一次註冊的同一個文本文件」abc.txt「意圖仍然相同。謝謝你的進一步建議 – manhon