Android: Passing Objects Between Fragments

前端 未结 4 1890
旧巷少年郎
旧巷少年郎 2021-01-05 04:00

Before i start, i have look through question such as:

Passing data between fragments: screen overlap How to pass values between Fragments

as well as Android

4条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-05 04:31

    I found a solution to my question, which i am sure not the correct way to do that...

    So in RegisterActivity.java i add + modified the following lines (thx to @sturrockad):

    public Registration getRegistrationData(){
        return this.regData;
    }
    
    public void onEmailRegistered(int position, Registration regData){
        this.regData = regData;
        viewPager.setCurrentItem(position, true);
    }
    

    Then in RegisterTwoFragments.java (or in the Fragment to which i want to receive the Object):

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
    
        View rootView = inflater.inflate(R.layout.fragment_register_two, container, false);
        regData = ((RegisterActivity) getActivity()).getRegistrationData();
        ...
    

提交回复
热议问题