Send data from activity to fragment using Tab Layout with Swipeable Views in Android

前端 未结 2 888
闹比i
闹比i 2021-01-29 05:21

I created a Tab Layout with Swipeable Views using this tutorial. I\'m trying to pass a string from Activity to Fragment. I read about fragment communication and couple other top

2条回答
  •  庸人自扰
    2021-01-29 06:07

    EDIT: My suggestion is to make the following constructor which takes Bundle object as an argument:

    public EnterExitFragment(Bundle bundle) {
        super();
        setArguments(bundle);
    }
    

    and call that constructor with bundle object as the argument instead of the default one in your program. That way you're sure to add the bundle before you attach it to your Activity.

    Additionally, if that doesn't work, add a Bundle attribute to your EEF class, initialize it within your constructor and refer to it rather than getArguments() method:

    public class EnterExitFragment extends Fragment{
        TextView tvFloor1, tvFloor2, tvEmail;
        Button btnSend;
        Integer floorId, segmentId, spaceId, ticketId;
        String floorName, segmentName, spaceName;
        String email;
        Bundle args;
    
        public EnterExitFragment(Bundle bundle) {
            super();
            args = bundle;
        }
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
    
            View rootView = inflater.inflate(R.layout.activity_enter_exit, container, false);
    
            tvEmail = ((TextView)rootView.findViewById(R.id.textEmail));
            tvFloor1 = ((TextView)rootView.findViewById(R.id.textFloor1));
            tvFloor2 = ((TextView)rootView.findViewById(R.id.textFloor2));
            new FloorFreeSpaces(EnterExitFragment.this).execute();
    
            btnSend = ((Button)rootView.findViewById(R.id.btnSend));
            btnSend.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    new Floor(EnterExitFragment.this).execute();
                    email = args.getString("email"); //line 55
                    tvEmail.setText(email);
                }
            });
            return rootView;
        }
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
        }
    

提交回复
热议问题