从startActivityForResult返回后,片段消失了 [英] Fragment disappear after coming back from startActivityForResult

查看:115
本文介绍了从startActivityForResult返回后,片段消失了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个奇怪的问题。

我在我的代码中使用了嵌套片段,(4级)

i use nested fragment in my code,( 4 level )

首页->服务->服务详细信息->上传

Home -> Services -> ServiceDetails -> Upload

在最后一个片段(上传片段)中,我想从图库或相机中选择图像,所以我写了以下内容选择图像的代码:

in the last fragment ( Upload Fragment ) i want to choose image from the gallery or the camera so i wrote the following code to pick the image :

switch (which) {
    case galleryItem:
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(
                Intent.createChooser(intent, "Select Album",
                Home.GALLERY_REQUEST);
        break;

    case cameraItem:
        Intent cameraIntent = new Intent(
                android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(cameraIntent,
                Home.CAMERA_REQUEST);
        break;

一切正常,
i可以从 onActivityResult ,其代码如下:

and every thing is ok. i can get URI from the selected picture in onActivityResult of my fragment with the following code:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {

    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == Activity.RESULT_OK) {
        if (requestCode == Home.GALLERY_REQUEST) {
            Uri selectedImageUri = data.getData();

        } else if (requestCode == Home.CAMERA_REQUEST) {
            Bitmap photo = (Bitmap) data.getExtras().get("data");
            Uri cameraUri = getImageUri(
                    getActivity().getApplicationContext(), photo);

        }
    }
}

问题

如果我打开我的相册并滚动(查看所有缩略图),然后选择图片 onActivityResult 在片段中调用,但片段不再可见,并且 Home 片段(第一个片段)在我的应用程序中可见。
,但如果我打开相册(通过startActivityForResult)并立即选择照片,则一切正常。

if i open my album and scroll ( see all thumbnail ) in that, after selecting my picture onActivityResult called in the fragment but fragment is not visible any more and Home fragment ( first fragment ) be visible in my app. but if i open album ( by startActivityForResult ) and immediately select photo, all thing is going ok.

我对相机没有任何问题。

i don’t any problem with camera.

我进行了很多搜索,但找不到任何有用的数据,如果您想查看代码的任何部分告诉我,则

我只丢了最后一个片段就没有崩溃。
预先感谢。

i searched a lot but don’t find any helpful data, if you want to see any part of my code tell me. i don’t get any crash just i lose my last fragment. thanks in advance.

推荐答案

我的问题已通过更改

 Intent intent = new Intent();
 intent.setType("image/*");
 intent.setAction(Intent.ACTION_GET_CONTENT);
 startActivityForResult(
        Intent.createChooser(intent, "Select Album",
        Home.GALLERY_REQUEST);

至:

Intent intent = new Intent(Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, Home.GALLERY_REQUEST);

我不知道为什么,但是如果有人知道通过 Intent.createChooser 启动意图和通常打电话告诉我之间有什么区别

i don't know why but if any one know what is different between starting intent by Intent.createChooser and normally call tell me, thanks

这篇关于从startActivityForResult返回后,片段消失了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆