Android 为结果启动活动而不返回调用活动

Android 为结果启动活动而不返回调用活动,android,android-fragments,android-intent,onactivityresult,startactivityforresult,Android,Android Fragments,Android Intent,Onactivityresult,Startactivityforresult,我有一个片段,它从图库中调用Select Image,如下所示 Intent intent=new Intent(); intent.setType("image/*"); intent.setAction(Intent.ACTION_GET_CONTENT); startActivityForResult(Intent.createChooser(intent,"Complete Action Using"),1); 我成功地执行了上述方法,启动了手机图库,我可以从中选择图像

我有一个片段,它从图库中调用Select Image,如下所示

  Intent intent=new Intent();
  intent.setType("image/*");
  intent.setAction(Intent.ACTION_GET_CONTENT);
  startActivityForResult(Intent.createChooser(intent,"Complete Action Using"),1);
我成功地执行了上述方法,启动了手机图库,我可以从中选择图像,但应用程序应该返回到调用片段,但它不是,甚至我可以在LogCat中看到调用结果的活动

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode==1 && resultCode==getActivity().RESULT_OK){

        Log.d(TAG,"Request Code is 1 Running: "+requestCode+" "+resultCode);
        Uri imageNameURL=data.getData();
        Log.d(TAG,imageNameURL.toString());
        ImagePath=getPath(imageNameURL);
        Log.d(TAG,"onActivityResult "+ImagePath);
        bitmap= BitmapFactory.decodeFile(ImagePath);

    }
    else if (requestCode==2 && resultCode==getActivity().RESULT_OK){

        Log.d(TAG,"Request Code 2 Running");
        LatLngBounds LL= PlacePicker.getLatLngBounds(data);
        double lat=LL.northeast.latitude;
        double longi=LL.northeast.longitude;
        double lat2=LL.southwest.latitude;
        double longi2=LL.southwest.longitude;            
        }

    }
我还有一个从映射返回到调用片段的get位置,但在选择图像时,我不会返回到调用活动,而是显示父活动的第一个片段

这是我的活动结果

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode==1 && resultCode==getActivity().RESULT_OK){

        Log.d(TAG,"Request Code is 1 Running: "+requestCode+" "+resultCode);
        Uri imageNameURL=data.getData();
        Log.d(TAG,imageNameURL.toString());
        ImagePath=getPath(imageNameURL);
        Log.d(TAG,"onActivityResult "+ImagePath);
        bitmap= BitmapFactory.decodeFile(ImagePath);

    }
    else if (requestCode==2 && resultCode==getActivity().RESULT_OK){

        Log.d(TAG,"Request Code 2 Running");
        LatLngBounds LL= PlacePicker.getLatLngBounds(data);
        double lat=LL.northeast.latitude;
        double longi=LL.northeast.longitude;
        double lat2=LL.southwest.latitude;
        double longi2=LL.southwest.longitude;            
        }

    }

有时在AndroidManifest.xml中启用noHistory=false可以工作。我认为这几乎是一件被遗忘的事情

怎么做?

打开AndroidManifest.xml文件,找到包含onActivityResult()方法的文件的标记,然后输入android:noHistory=“false”

像这样

<activity
        android:name="MyActivityClassName"
        android:screenOrientation="portrait"
        android:noHistory="false"
        />


希望它能帮助其他人。

调用活动在尝试加载位图时可能会崩溃。您应该发布带有任何错误消息的日志,并准确描述发生的情况,因为我不确定我是否理解您目前发布的内容。日志中没有错误,我只能看到on activity result正在调用,但我的地图活动返回到相同的片段,并且我在logcat中看到图像on activityresult log不要过滤logcat,也许你会看到相关的异常或崩溃。@DavidWasser我总是将日志设置为verbose我不是这个意思。我的意思是不要按应用程序的包名过滤。大多数IDE使用应用程序的包名自动设置logcat过滤器。如果您使用这种类型的过滤器,您将错过android框架生成的任何异常或消息,而这些消息通常正是您需要向正确方向指出以解决问题的消息。