当 Preference Fragment 将其作为父级调用时,不会调用 onActivityResult

问题描述

我的应用程序记录 GNSS 位置,作为使用手机制作定向越野地图的工具。主要活动完成所有工作,当映射器需要导出完成的现场工作时,应用程序调用一个子活动 (Save_File),负责使用 gpx 格式在 Documents/Oribooklet 目录中保存文件。由于 getExternalStoragePublicDirectory 已过时,因此 SAF 是可以选择的方式。我不是 IT 人员,我这样做是为了好玩,这就是为什么我这么晚才处理这件事:用户给我发消息说他们不能再保存现场工作了。

主要:Oribooklet

public class Oribooklet extends AppCompatActivity {

...

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Mapper clicked on a menu option in the app bar overflow menu
    Intent intent;
    switch (item.getItemId()) {

        // Respond to a click on the "Save" menu option
        case R.id.action_saveFile:
            // Call the Save_File Class
            // Prepare to show Save UI

            // Save to Shared Preference
            SharedPreferences sharedPref = getSharedPreferences("user_preferences",MODE_PRIVATE);
            SharedPreferences.Editor editor = sharedPref.edit();
            editor.putBoolean("bShowSaveUI",true);
            editor.apply();

            intent = new Intent(Oribooklet.this,Save_File.class);
            startActivity(intent);
            return true;

        // Respond to a click on the "Preference" menu option
        case R.id.action_settings:
            // Call the Class Activity_Preference for options menu
            intent  = new Intent(Oribooklet.this,Activity_Preference.class);
            startActivity(intent);
            return true;
    }
    return super.onOptionsItemSelected(item);
}

在我实现的 Save_File 活动中,它工作正常:当映射器推送保存时,startActivityForResult 被触发,然后 Android 文件选择器启动,映射器选择保存位置。

void createExternalStoragePublicDocument(String fileName) {

    try {
        // Call SAF
        Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        intent.setType(INTENT_TYPE);
        intent.putExtra(Intent.EXTRA_TITLE,fileName);

        // https://stackoverflow.com/questions/6147884/onactivityresult-is-not-being-called-in-fragment
        startActivityForResult(intent,CREATE_REQUEST_CODE);
    } catch (Exception excep) {
        excep.printStackTrace();
    }
}

然后 onActivityResult 获取返回的 Uri 以通过使用在现场收集的数据填充文件来完成作业。再好不过了!

    @Override
public void onActivityResult(int requestCode,int resultCode,Intent resultData) {
    super.onActivityResult(requestCode,resultCode,resultData);
    Uri currentUri = null;
    if (resultCode == Activity.RESULT_OK) {
        if (requestCode == CREATE_REQUEST_CODE) {
            if (resultData != null) {
                currentUri = resultData.getData();
                // Write contents
                writeFileContent(currentUri,fileData);
            }
        } else if (requestCode == OPEN_REQUEST_CODE) {
            if (resultData != null) {
                currentUri = resultData.getData();
                sendByMail(stringEMailAddress,stringSubject,currentUri);
                // Back to Oribooklet
                finish();
            }
        }
    }
}

该应用程序有一个 Preference 片段来设置一些参数。其中之一是要使用的标准。两种标准不得混用做地图。当映射器更改地图类型时,Preference 会调用 Save_File 来完成这项工作,因此不会发生数据混合。它曾经工作得很好。

@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,final String key) {
    // Find the one that changed and address it
    Preference connectionPref = findPreference(key);
    switch (key){
        case "list_iof":
            // IOF´s element set
            SharedPreferences preferences = getActivity().getSharedPreferences("user_preferences",MODE_PRIVATE);

            // Update preference Summary
            ListPreference listIOFPreference = (ListPreference) findPreference("list_iof");
            CharSequence iof = listIOFPreference.getEntry();
            connectionPref.setSummary(getResources().getString(R.string.pref_iof_summary_1) +
                    iof + "\" " +
                    getResources().getString(R.string.pref_iof_summary_2));
            // Save the field job to avoid IOF set mixing
            Intent intent = new Intent(getActivity(),Save_File.class);
            startActivity(intent);
            break;

在 SAF 之后,Save_File 的 onActivityResult 永远不会被触发,startActivityForResult 会触发文件提供者,但是当映射器选择保存位置时,它返回到 Preference,而不是 Save_File 的 onActivityResult,因此文件被创建为空。

我做了很多测试,调试了很多断点,在很多地方设置和删除了super,检查了所有的S.O.关于此事的帖子没有成功。

有什么想法吗?

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.INTERNET"/>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_oribooklet_v0"
android:name=".MyAppContext"

android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
    <!--android:requestLegacyExternalStore="true" -->

    <!-- android:roundIcon="@mipmap/ic_oribooklet_v2"-->

    <activity android:name="com.hbcavalcanti.oribooklet.Oribooklet"
              android:theme="@style/Theme.AppCompat.Light.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <!-- A child of the main activity -->
    <activity
        android:name=".Save_File"
        android:label="@string/action_saveFile"
        android:parentActivityName=".Oribooklet" >

        <!-- Parent activity meta-data to support 4.0 and lower -->
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".Oribooklet" />
    </activity>

    <activity
        android:name=".Activity_Preference"
        android:label="@string/action_settings"
        android:parentActivityName=".Oribooklet" >

        <!-- Parent activity meta-data to support 4.0 and lower -->
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".Oribooklet" />
    </activity>

    <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="com.hbcavalcanti.oribooklet.fileprovider"
        android:grantUriPermissions="true"
        android:exported="false">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/filepaths" />
    </provider>
</application>

解决方法

当从 Preference 调用时,Save File 方法过去不进行任何 UI 调用,因此它的 onActivityResult 从未被调用。

仅出于测试目的,我在 Preference 片段上引入了一个 onActivityResult 并且它收到了一个触发器。测试后我删除了。

我通过在 Preference 调用时在 Save File 中调用 UI 来解决这个问题。这会触发其 onActivityResult 使一切正常,就像从其父级调用一样。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...