Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Communication between Fragments

    • 0
    • 1
    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 770
    Comment on it

    Here I will explain how we can communicate between two fragments in an Activity. There are many situations where we have to communicate two fragments like we have to pass data between fragments in some events. Here I will communicate through Interface but we need to create an Interface first.

    ActivityInterface.java

    Create an interface which will help us to communicate.

    interface Activityinterface { 
      public void passInfo(); 
    } 
    

    Now our Activity ScreenActivity implements ActivityInterface

    public class ScreenActivity extends FragmentActivity implements ActivityInterface
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_screen); 
    } 
    
    @Override 
    public void passInfo () { 
        // TODO Auto-generated method stub 
    
    
    // We can call this from Fragment by using ActivityInterface reference. 
    FragmentManager fragmentManager = getSupportFragmentManager(); SecondFragment secondFragment = (SecondFragment) fragmentManager.findFragmentById(R.id.second_fragment); secondFragment.changesOccur();

    } }
    public class FirstFragment extends Fragment implements OnClickListener{ 
     Button changeButton; 
     ActivityInterface activityInterface; 
        @Override 
        public View onCreateView(LayoutInflater inflater, ViewGroup container, 
    Bundle savedInstanceState) { // TODO Auto-generated method stub

        return inflater.inflate(R.layout.layout_fragmenta, container, false); 
    } 
    
    
    
    @Override 
    public void onActivityCreated(Bundle savedInstanceState) { 
        // TODO Auto-generated method stub 
    

    super.onActivityCreated(savedInstanceState); activityInterface = (activityInterface) getActivity(); changeButton = (Button) getActivity().findViewById(R.id.change_button); changeButton.setOnClickListener(this); }

    @Override public void onClick(View v) { // TODO Auto-generated method stub

    activityInterface. passInfo (); 
    

    } }

    public class SecondFragment extends Fragment{ 
    int valueCountng; 
    TextView countAmount; 
        @Override 
        public View onCreateView(LayoutInflater inflater, ViewGroup container, 
    Bundle savedInstanceState) { // TODO Auto-generated method stub return inflater.inflate(R.layout.second_fragment, container, false); } @Override public void onActivityCreated(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onActivityCreated(savedInstanceState); countAmount = (TextView)getActivity().findViewById(R.id.count_amount); } public void changesOccur (){ String str = countAmount.getText().toString(); valueCountng = Integer.parseInt(str); valueCountng ++; countAmount.setText(String.valueOf(valueCountng)); } }

 0 Comment(s)

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: