Cannot control Progressbar visibility from onActivityResult in Fragment

試著忘記壹切 提交于 2020-01-05 04:40:36

问题


I have a Fragment which contains a Progressbar. I retrieve it in onCreateView() method where setVisibility() works fine.

Now, when I try to set visibility of the same progressbar (declared in fragment at class level) inside onActivityResult() nothing happens. Here is the code.

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data){
if(requestCode == REQUEST_CODE_PROFILE_VIDEO_PATH){
        if(resultCode == Activity.RESULT_OK) {
            String profileVideoPath = data.getExtras().getString(ProfileVideoRecordingActivity.VIDEO_PROFILE_PATH);
            Log.d("DEBUG", profileVideoPath);

            //Upload to server
            File profileVideo = new File(profileVideoPath);
            if(profileVideo.exists()) {

                pbarVideoUpload.setVisibility(View.VISIBLE);
                FireBaseWrapper fileUploader = new FireBaseWrapper();
                String serverFolderPath = "videoprofile";
                String contentType = "video/mp4";
                FireBaseAfterUpload afterUpload = new FireBaseAfterUpload() {
                    @Override
                    public void onSuccess(String uploadURL) {
                        Log.d("DEBUG", "Successfully uploaded video to server");
                        Log.d("DEBUG", uploadURL);
                        pbarVideoUpload.setVisibility(View.GONE);

                        ProfileService profileService = new ProfileService(TAG) {
                            @Override
                            protected void onPreServiceCall() {

                            }

                            @Override
                            protected void onPostServiceCall() {

                            }

                            @Override
                            public void afterSuccess(Object object) {
                                ReturnCode successCode = (ReturnCode) object;
                                if(successCode.getSuccess()){
                                    Log.d("DEBUG", "Profile Video URL updated in DB");
                                }else{
                                    Log.d("DEBUG", "Profile Video URL NOT updated in DB");
                                }
                            }

                            @Override
                            public void afterError() {
                                Log.d("DEBUG", "Profile Video URL NOT updated in DB");
                            }
                        };
                        profileService.updateVideoPath(uploadURL);

                    }

                    @Override
                    public void onProgress(String data) {

                    }

                    @Override
                    public void onFaliure() {
                        Log.d("DEBUG", "Error! Didn't upload");
                        pbarVideoUpload.setVisibility(View.GONE);
                    }
                };

                try {
                    fileUploader.upload(profileVideo, profileVideo.getName(), serverFolderPath, afterUpload, contentType, false);
                }catch (FileNotFoundException e){
                    Log.e("DEBUG", "FileNotFoundException", e);
                }
            }

        }
    }
}

I tried calling setVisibility() inside an Handler and also on UI thread using runOnUiThread(). Both approaches didn't work.

How can I control visibility of progressbar inside onActivityResult() of Fragment?

I need it as I am uploading a file inside onActivityResult() and need to display progress.


回答1:


I think you onActivityResult not triggered as it is in fragment. Please use the below code in your activity which hold the fragment.

public void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
Fragment fragment = (Fragment) getSupportFragmentManager().findFragmentByTag(fragmentTag);
if (fragment != null) {
    fragment.onActivityResult(requestCode, resultCode, intent);
}
}



回答2:


put this

  pbarVideoUpload.setVisibility(View.GONE);

Inside this also like I mentioned,

                            @Override
                            public void afterSuccess(Object object) {
                                ReturnCode successCode = (ReturnCode) object;
                                if(successCode.getSuccess()){
                                  pbarVideoUpload.setVisibility(View.GONE);
                                    Log.d("DEBUG", "Profile Video URL updated in DB");
                                }else{
                                    Log.d("DEBUG", "Profile Video URL NOT updated in DB");
                                }
                            }



回答3:


Fragments onCreate() was called after onActivityResult(). Hence the views were getting reinitialized and progressbar was not displayed.

Created a static variable isVideoUploading. Set its values appropriately in onActivityResult() and used it to show/hide progressbar in onCreate(). This solved half the problem. The progressbar was now visible when uploading started.

Second half of the problem was to hide the progressbar on complete of firebase async upload method. Problem was that since upload was being done in background thread which started in onActivityResult() there was no way for onCreateView() to fire again after upload was complete. For this I sent a broadcast intent using LocalBroadcastManager when upload was complete and registered it in the same fragment. Once broadcast was received I hid the progressbar.



来源:https://stackoverflow.com/questions/50327949/cannot-control-progressbar-visibility-from-onactivityresult-in-fragment

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