我有一个包含Progressbar的Fragment。我使用onCreateView()
方法检索setVisibility()
正常工作。
现在,当我尝试在onActivityResult()
内设置相同进度条(在类级别的片段中声明)的可见性时,没有任何反应。这是代码。
@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);
}
}
}
}
}
我尝试在Handler中调用setVisibility()
,并使用runOnUiThread()
调用UI线程。这两种方法都不起作用。
如何控制片段onActivityResult()
内的进度条的可见性?
我需要它,因为我在onActivityResult()
内上传文件并需要显示进度。
答案 0 :(得分:0)
我认为onActivityResult没有被触发,因为它在片段中。请在活动中使用以下代码来保存片段。
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);
}
}
答案 1 :(得分:0)
把这个
pbarVideoUpload.setVisibility(View.GONE);
在这里也像我提到的那样,
@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");
}
}
答案 2 :(得分:0)
在onActivityResult()之后调用onCreate()片段。因此,意见重新初始化,并且没有显示进度条。
创建了一个静态变量isVideoUploading。在onActivityResult()中适当设置其值,并使用它在onCreate()中显示/隐藏进度条。 这解决了一半的问题。现在可以在上传开始时看到进度条。
问题的后半部分是在完成firebase异步上传方法时隐藏进度条。问题是,由于上传是在onActivityResult()中启动的后台线程中完成的,因此上传完成后onCreateView()无法再次触发。 为此,我在上传完成后使用LocalBroadcastManager发送了一个广播意图,并将其注册在同一个片段中。收到广播后,我隐藏了进度条。