Fragment's onActivityResult not called after orientation change

一笑奈何 提交于 2019-12-03 06:12:22

i Have one Solution it is currently Working in my project. follow Step.

--> Create Interface name OnActivityResultListener(or name as you wish).

public interface OnActivityResultListener {

public boolean onActivityResultTest(int request_code, int response_code,
        Intent data);

}

--> Make refrence of intercace in your FragmentActivity like

public OnActivityResultListener activityResultListener;

--> now override OnActivityResult in FragmentActivity. like below.

@Override
public void onActivityResult(int arg0, int arg1, Intent arg2) {
    AppLog.Log(TAG, "onActivityResult");
    try {
        if (activityResultListener != null) {
            activityResultListener.onActivityResultTest(arg0,arg1,arg2);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    super.onActivityResult(arg0, arg1, arg2);
    }

--> now implement interface in your Fragment and override required method.

@Override
public boolean onActivityResultTest(int requestCode, int resultCode,
        Intent data) {
    AppLog.Log(TAG, "onActivityResultTest");
    if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {
        isHavePhoto = true;
        bitmap = (Bitmap) data.getExtras().get("data");
        userEditPhoto.setImageBitmap(bitmap);
    }
}

--> and the last step is inside OnAttach Method of Fragment class is register your Fragment in activity for the the Activity result. like

@Override
    public void onAttach(Activity activity) {
        this.activity = (YOUR_FRAGMENT_ACTIVITY) activity;
            this.activity.activityResultListener = this;
        super.onAttach(activity);
    }

-->now test and don't forgot to remove it

@Override
public void onDestroyView() {
    activity.activityResultListener = null;
    super.onDestroyView();
}

->it is some what lengthy process but it is working 100% in any orientation.

Resolved it by moving all of the onActivityResult logic from the Fragment to the Activity.

Do note, however, that the reqCode that gets passed to the Fragment is different than the reqCode that gets delivered to the parent Activity (the one for the Activity is set by the system I think). This meant that I had to also move the original startActivityForResult call from the Fragment to the Activity, and then create a method to call it from the Fragment when needed.

If anyone finds a more elegant way, I would be happy to hear about it.

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