Fragment disappear after coming back from startActivityForResult

纵饮孤独 提交于 2019-12-23 01:53:02

问题


i have a strange problem.

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;

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);

        }
    }
}

Problem

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.


回答1:


Explicit call from fragment to onActivityResult function as follows

In Parent Activity class, override the onActivityResult() method and even override the same in Fragment Class and call as the following code.

In Parent Class:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.dualPane);
fragment.onActivityResult(requestCode, resultCode, data);
}

In Child Class:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
   //in fragment class callback
    }



回答2:


my problem has been solved by changing

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

to :

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

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



来源:https://stackoverflow.com/questions/27592113/fragment-disappear-after-coming-back-from-startactivityforresult

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!