在backpress上重新加载整个片段

时间:2018-09-10 10:19:11

标签: android android-studio android-fragments

我有一个AppCompatActivity,允许用户绘制签名,然后活动将签名的路径传递给fragmentfragment应该将签名放在ImageView上。 fragment正在接收图像的正确路径。为了进行测试,当我手动放置路径时,ImageView在App Startup上很好地显示了图像。但是从活动中提取了签名后,该图像没有显示在fragment上。
 在研究了很多关于stackoverflow的常见问题解答之后,我知道fragmentbackpress起,activity组件并没有刷新。 fragment上有一个条款和条件复选框。从checkbox返回后,即使activity仍然被检查。在解决这个问题上花费了几个小时。没有运气。

活动侧编码以传递图像路径-

Menu1_SecondClass fragment = new Menu1_SecondClass(); 
fragment.setBoolean(true);
Bundle bundle = new Bundle();
bundle.putString("imagePath", StoredPath);  // Passing the Path
fragment.setArguments(bundle);

FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.linearLayout, fragment);
ft.commit();

finish();
mFileOutStream.flush();
mFileOutStream.close();

片段侧面编码

public class Menu1_SecondClass extends Fragment {
    Button signatureButton;
    ImageView signImage;
    CheckBox checkBox;
    View view;
    public String image_path;
    public boolean vboolean = false;

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        getActivity().setTitle("Homework Details");
    }
    public void setBoolean(Boolean boo){
        this.vboolean = boo;
    }
    public boolean getBoolean(){
        return this.vboolean;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(getBoolean()){
            Toast.makeText(getActivity(), "Refreshing.. " , Toast.LENGTH_SHORT).show();
            this.setBoolean(false);
            refreshFragment();
        }
    }

    public void refreshFragment(){
        FragmentTransaction t = getActivity().getSupportFragmentManager().beginTransaction();
        t.setReorderingAllowed(false);
        t.detach(this).attach(this).commitAllowingStateLoss();
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.activity_homework, container, false);

        signatureButton = (Button) view.findViewById(R.id.getSign);
        signImage = (ImageView) view.findViewById(R.id.imageViewSign);
        signatureButton.setOnClickListener(onButtonClick);
        checkBox = (CheckBox) view.findViewById(R.id.checkbox);

        //disable button if checkbox is not checked else enable button
        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    signatureButton.setEnabled(true);
                } else {
                    signatureButton.setEnabled(false);
                }
            }
        });
        putSignature();

        return view;
    }

    public void putSignature(){

        Bundle bundle = this.getArguments();

        if (bundle != null) {
            image_path = bundle.getString("imagePath", "");                if(!image_path.isEmpty()) {
                Bitmap bitmap = BitmapFactory.decodeFile(image_path);
                if (bitmap != null) {
                    //bitmap = Bitmap.createScaledBitmap(bitmap, 120, 80, false);
                    signImage.setImageBitmap(bitmap);
                    Toast.makeText(getActivity(), "Image Created: " + image_path, Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(getActivity(), "Null Image", Toast.LENGTH_SHORT).show();
                }
            }
        }
    }

    Button.OnClickListener onButtonClick = new Button.OnClickListener() {
        @Override
        public void onClick(View v) {

            if (checkBox.isChecked()) {
                Intent i = new Intent(getActivity(), SignatureActivity.class);
                startActivity(i);
            } else {
                signatureButton.setEnabled(false);
            }
        }
    };
}

2 个答案:

答案 0 :(得分:0)

当您将数据从activity传递到fragment时,可以使用以下代码

Bundle bundle = new Bundle();
bundle.putString("imagePath", imagePath);
// set Fragmentclass Arguments
Fragmentclass fragobj = new Fragmentclass();
fragobj.setArguments(bundle);

并且在fragment方法内以onCreateView接收数据的同时,您可以编写这样的代码

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
    String strtext = getArguments().getString("imagePath");
    return inflater.inflate(R.layout.fragment, container, false);
}

答案 1 :(得分:0)

transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.framelay_loan, firstFrag);
transaction.addToBackStack(fragment.getClass().getName());
Bundle bundle = new Bundle();
bundle.putString("imagePath", imagePath);
fragment.setArguments(bundle);
transaction.commit();`

在onCreateView方法内部

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState ) {
      View view = inflater.inflate(R.layout.fragment_micro_form1, container,false);
      Bundle bundle = this.getArguments();
        if (bundle != null) {
            image_path = bundle.getString("imagePath", ""); 
        }

        return view;
    }