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

前端 未结 2 885
闹比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);
        }
    
    0 讨论(0)
  • 2021-01-29 06:08

    I do not see that you set your argument to fragment. If you did, but you forgotten to write please inform me...

    Bundle bundle=new Bundle();
    bundle.putString("email", "emasiofnasdbjk");
    EnterExitFragment enterExitFragment = new EnterExitFragment();
    enterExitFragment.setArguments(bundle); // This line is needed...
    android.support.v4.app.FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    transaction.replace(R.id.pager, enterExitFragment);
    transaction.addToBackStack(null);
    transaction.commit();
    
    0 讨论(0)
提交回复
热议问题