Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to pass data from one fragment to another in Android?

    • 0
    • 0
    • 0
    • 0
    • 3
    • 0
    • 0
    • 0
    • 32.6k
    Comment on it

    To pass data from one fragment to another in android we simply make use of Bundle .

    In this example I am sending three String  from MainActivityFragment(Fragment)  to SecondFragment(Fragment) with the help of Bundle. 

    Below is the code for the MainActivityFragment.

     

    MainActivityFragment.java

    public class MainActivityFragment extends Fragment {
    
    private EditText memail_et,muser_name_et,mphone_et;
    private Button sign_up_btn;
    private String memail,muser_name,mphone_number;
    
    @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            View rootView=inflater.inflate(R.layout.fragment_main_activity, container, false);
    
            memail_et=(EditText)rootView.findViewById(R.id.email_et);
            muser_name_et=(EditText)rootView.findViewById(R.id.user_name_et);
            mphone_et=(EditText)rootView.findViewById(R.id.mobile_no_et);
            sign_up_btn=(Button)rootView.findViewById(R.id.sign_up_btn);
            sign_up_btn.setOnClickListener(mClickListener);
                  return rootView;
        }
    View.OnClickListener mClickListener=new View.OnClickListener() {
        @Override
        public void onClick(View v) {
          
        memail=memail_et.getText().toString();
                muser_name=muser_name_et.getText().toString();
                mphone_number=mphone_et.getText().toString();
    
         FragmentTransaction transection=getFragmentManager().beginTransaction();
                SecondFragment mfragment=new SecondFragment();
    
    //using Bundle to send data 
                Bundle bundle=new Bundle();
                bundle.putString("email",memail);
                bundle.putString("user_name",muser_name);
                bundle.putString("phone",mphone_number);
                mfragment.setArguments(bundle); //data being send to SecondFragment
                transection.replace(R.id.main_fragment, mfragment);
                transection.commit();
      }
    
        }
    
    }
    

     

    Add the below code to your SecondFragment.

     

    SecondFragment.java

    public class SecondFragment extends Fragment {
    
    private TextView memail,mphone,muser;
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            View rootview = inflater.inflate(R.layout.fragment_second, container, false);
            memail=(TextView)rootview.findViewById(R.id.mail);
            mphone=(TextView)rootview.findViewById(R.id.phone_number);
            muser=(TextView)rootview.findViewById(R.id.user_name);
    
    //retrieving data using bundle
    
            Bundle bundle=getArguments();
            memail.setText(String.valueOf(bundle.getString("email")));
            muser.setText(String.valueOf(bundle.getString("user_name")));
            mphone.setText(String.valueOf(bundle.getString("phone")));
            return rootview;
        
    }
    
    
    
    }

     

 3 Comment(s)

  • Thank you so much , Very simple and clear understanding. Worked perfect . 
    I am still confuse in one concept, Camera access code in Fragment. Not working. 
    IIT would be great If you can demostrate that. 

     
Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: