在片段Android中更新ImageView

时间:2017-07-25 07:59:25

标签: java android android-fragments

我有一个MainActivity,上面几个片段。

我正在尝试使用Gallery更改片段中的ImageView。但是一旦选择了图像,MainActivity就会刷新,这会将MainActivity更改回默认片段。

获得许可

 textViewChangeImage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d(TAG,"Select Image executed");

            requestPermissions(
                    new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                    REQUEST_CODE);



        }
    });

请求许可

 @Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    Log.d(TAG,"Select Image Permission executed");

    if(requestCode == REQUEST_CODE){
        progressDialogUploadDp.setMessage("Uploading...");
       progressDialogUploadDp.show();

        if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
            Intent intent = new Intent(Intent.ACTION_PICK);
            intent.setType("image/*");
            intent.putExtra("imageUpdate","true");
            startActivityForResult(intent,REQUEST_CODE);
        }else{
            Toast.makeText(getActivity(), "No Permission Granted", Toast.LENGTH_SHORT).show();
        }
        return;
    }

    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}

关于活动结果

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

    super.onActivityResult(requestCode, resultCode, data);

    if(requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK && data != null){

        try {
        Uri uri = data.getData();

        StorageReference filepath =
                storageReference.child("Photos").child("DPs").child(ID).child("profilepicture.jpg");
            InputStream inputStream = getActivity().getContentResolver().openInputStream(uri);

            Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
            imageviewpropic.setImageBitmap(bitmap);

            filepath.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

                    progressDialogUploadDp.hide();
                    progressDialogUploadDp.dismiss();


                }
            });

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }


    }

期待解决方案!

更新---

MainActivity

@Override
public void onStart() {
    super.onStart();
        mAuth.addAuthStateListener(mAuthListener);
}

@Override
public void onStop() {
    super.onStop();
    if (mAuthListener != null) {
        mAuth.removeAuthStateListener(mAuthListener);
    }
}

@Override
protected void onResume() {
    super.onResume();

}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if(savedInstanceState==null) {
mRef.addValueEventListener(new ValueEventListener() {
                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {
                            if (dataSnapshot.hasChild("questioners")) {
adapter = new SectionStatePagerAdapter(getSupportFragmentManager());
                                    adapter.addFragment(new HomeFragment(), "HomeFragment");
                                    viewPager.setAdapter(adapter);
}}
}

2 个答案:

答案 0 :(得分:1)

返回表单库,活动将在onActivityResult上运行,然后它将运行onResume。 所以你可以在onActivityResult上设置一个FLAG,并在onResume中创建一个IF来设置你的片段。

答案 1 :(得分:0)

根据您的说法,您的活动似乎正在调用onCreate方法并再次设置默认片段。你可以做的是在onCreate()中只有在没有保存状态时才创建新片段。

if(savedInstanceState==null) setFragment(fragment);