Search code examples
androidandroid-fragmentsandroid-fragmentactivity

How to send information of fragment to activity and In reverse in android


How can I send information of activity to fragment and of fragment to activity in android?


Solution

  • Your question is very BAD and not clear! Please before ask any question, first read this link then ask question.

    But for use custom method in Activity from Fragment, you can use my below example.

    First write your Method in your activity:

    public void showMessage(){
        Toast.makeText(context, "YOUR MESSAGE", Toast.LENGTH_SHORT).show();
    }
    

    Then, you should create instance of activity in your Fragment :

    public class YOUR_FRAGMENT_NAME extends Fragment {
       private YOUR_ACTIIVTY_NAME yourActivity;
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            View view = inflater.inflate(R.layout.fragment_layout, container, false);
    
            //Initialize
            mainActivity = (MainActivity) getActivity();
    
            //Call method 
            yourActivity.showMessage();
    
        return view;
    }
    

    I hope help you.