Linked Questions

Popular Questions

How to invoke the method in one fragment from another fragment

Asked by At

I am using a container fragment(main) which contain another two fragments(let say A and B) , the main fragment contains some methods which need to be invoked when user presses a button inside either of the two fragments (A and B).

I have used the static method (and then invoke these methods by making object of main Fragment) and it runs fine, but it is not a best practice and also degrades the performance of the mobile app. Someone told me to use interface to invoke the method, but I do not know how to use interface to achieve my task. code of my main fragment fragment which need to be invoke

public void updateEditText(int i) {

    switch (i) {
        case 1:
            bottomSheetBehaviorTeacher.setState(BottomSheetBehavior.STATE_EXPANDED);
            break;
        case 2:
            bottomSheetBehaviorStudent.setState(BottomSheetBehavior.STATE_EXPANDED);
            break;
        case 3:
            bottomSheetBehaviorAdmin.setState(BottomSheetBehavior.STATE_EXPANDED);
            break;


    }

}

when user press these button in frgament A

addTeachers.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

          //listener.onInputASent(1);

        }
    });

    addStudents.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            //listener.onInputASent(2);
        }
    });

and in fragment B

addAdminbtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            //mainLayoutFragment.BottomSheet_layout_admin();
            //listener.onInputASent(3);
        }
    });

Related Questions