0

我已将我的应用与 txt 文件相关联。所以用户可以在文件资源管理器应用程序中选择一个文件,然后选择我的应用程序来打开它。在 OnCreate 中:

    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 为空。为什么?非常感谢您的帮助。

4

1 回答 1

0

不要在 onResume() 方法中创建新的 Intent 对象,而是使用您在 onCreate 方法中使用的现有对象。

     Intent intent; 

创建:

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

简历:

String action = intent.getAction();        
if (action != null && action.equals(Intent.MAIN)) {
    txtFilePath = intent.getData().getPath();
}
于 2013-11-03T15:49:44.237 回答