Fragments Android Studio

huangapple 未分类评论78阅读模式
英文:

Fragments Android Studio

问题

下午好,

我无法通过按下返回按钮来恢复片段中的数据。

我的束带没问题,结果正常显示,但当回到 cpf 屏幕时,值未应用。

我正在尝试分阶段创建注册表单,每个片段都是要填写的字段。

// 邮件片段
btnBack.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        bundle = new Bundle();
        bundle.putSerializable("object", user);
        Log.i("Bundle", bundle.toString());
        Navigation.findNavController(v).navigate(R.id.cpfFragment, bundle);
    }
});

无法恢复数据的片段

public class CpfFragment extends Fragment {

    private User user;
    private Bundle bundle;

    public CpfFragment() {
        // 必要的空公共构造函数
    }

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

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

        if(savedInstanceState != null){
            user = (User) bundle.getSerializable("object");
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_cpf, container, false);
    }

    @Override
    public void onViewCreated(@NonNull final View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        Button btnNext = view.findViewById(R.id.btnNext);
        Button btnBack = view.findViewById(R.id.btnBack);

        final EditText fieldCpf = view.findViewById(R.id.editCpf);


        btnNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String cpf = fieldCpf.getText().toString();

                try {
                    if (cpf.isEmpty()){
                        Toast.makeText(getContext(), "Preencha o Cpf para avançar", Toast.LENGTH_SHORT).show();
                    }
                    else{

                        user = new Usuario();
                        user.setCpf(cpf);
                        Log.i("CPF", user.getCpf());

                        bundle = new Bundle();
                        bundle.putSerializable("object", user);

                        Navigation.findNavController(v).navigate(R.id.emailFragment, bundle);
                    }
                }
                catch (Exception e ){
                    Toast.makeText(getContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
                    Log.i("ERROR BAGME CPF", e.getMessage());
                }
            }
        });

        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Navigation.findNavController(v).navigate(R.id.initialFragment);
            }
        });
    }
}
英文:

Good afternoon,

I am not able to recover the data from the fragments by pressing the back button.

My bandle is ok, the result appears normally, but when it comes back to the cpf screen the value is not applied

I'm trying to create a registration form in stages, each fragment is a field to be written

//fragment Email
btnBack.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        bundle = new Bundle();
        bundle.putSerializable("object", user);
        Log.i("Bundle", bundle.toString());
        Navigation.findNavController(v).navigate(R.id.cpfFragment, bundle);
    }
});

fragment that I can't recover the data

public class CpfFragment extends Fragment {

    private User user;
    private Bundle bundle;

    public CpfFragment() {
        // Required empty public constructor
    }

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

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

        if(savedInstanceState != null){
            user = (User) bundle.getSerializable("object");
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_cpf, container, false);
    }

    @Override
    public void onViewCreated(@NonNull final View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        Button btnNext = view.findViewById(R.id.btnNext);
        Button btnBack = view.findViewById(R.id.btnBack);

        final EditText fieldCpf = view.findViewById(R.id.editCpf);


        btnProximo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String cpf = fieldCpf.getText().toString();

                try {
                    if (cpf.isEmpty()){
                        Toast.makeText(getContext(), "Preencha o Cpf para avançar", Toast.LENGTH_SHORT).show();
                    }
                    else{

                        user = new Usuario();
                        user.setCpf(cpf);
                        Log.i("CPF", user.getCpf());

                        bundle = new Bundle();
                        bundle.putSerializable("object", user);

                        Navigation.findNavController(v).navigate(R.id.emailFragment, bundle);
                    }
                }
                catch (Exception e ){
                    Toast.makeText(getContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
                    Log.i("ERROR BAGME CPF", e.getMessage());
                }
            }
        });

        btnVoltar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Navigation.findNavController(v).navigate(R.id.initialFragment);
            }
        });
    }
}

答案1

得分: 0

你不要通过捆绑返回数据。在你的片段中创建一个接口,然后在你的活动中实现它,并在onAttach方法中将你的活动保存为该接口。然后让活动处理所有的事情,比如保存数据和显示下一个片段。

更多信息和来源:https://developer.android.com/guide/components/fragments#EventCallbacks

英文:

You don't return data over bundles. Create a interface in your fragment implement it in your activity and save your activity as said interface in the onAttach method. Then let the activity handle all stuff like saving the data and showing the next fragment.

More info and source: https://developer.android.com/guide/components/fragments#EventCallbacks

答案2

得分: 0

你可以创建一个单例类,并使用 getter 和 setter 来保存这些值。

英文:

You can make a singleton class and save the values using getter and setter.

huangapple
  • 本文由 发表于 2020年8月14日 23:36:49
  • 转载请务必保留本文链接:https://java.coder-hub.com/63415850.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定