How to make a callback between Activity and Fragment?

I have this interface in my work.

public interface LogoutUser { void logout(); } 

My snippet implements this interface, so in my snippet I have the following:

 @Override public void logout() { // logout } 

In my activity I call

 mLogoutUser.logout(); 

Where mLogoutUser is of type LogoutUser interface.

My problem is the mLogoutUser object, which is null. How to initialize it?

Thanks!

+6
source share
3 answers

Android snippets - communication with actions

You need to get a link to your fragment using getFragmentById() or getFragmentByTag()

 getFragmentManager().findFragmentById(R.id.example_fragment); 
+4
source

As I said in my comment, I solved this problem using the onAttach method in my fragment, but this way you should have a callback field (mLogoutUser in this case) declared in the fragment and initialize it this way:

 public class MyFragment extends ListFragment { LogoutUser mLogoutUser; // Container Activity must implement this interface public interface LogoutUser { public void logout(); } @Override public void onAttach(Activity activity) { super.onAttach(activity); // This makes sure that the container activity has implemented // the callback interface. If not, it throws an exception try { mLogoutUser = (LogoutUser) activity; } catch (ClassCastException e) { throw new ClassCastException(activity.toString() + " must implement LogoutUser"); } } ... } 

Additional information in Communication with other fragments .


But if your case is a field declared in an action, you can use the onAttachFragment method from your activity to initialize the listener field this way:

 @Override public void onAttachFragment(Fragment fragment) { super.onAttachFragment(fragment); mLogoutUser = (LogoutUser) fragment; } 

In addition, you can use the event bus to exchange data between fragments and actions. The option is Otto's library , from Square.

+9
source

Example of creating a callback from fragment to action

 public interface CallBackListener { void onCallBack();// pass any parameter in your onCallBack which you want to return } 

CallBackFragment.class

 public class CallBackFragment extends Fragment { private CallBackListener callBackListener; public CallBackFragment() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_call_back, container, false); } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); //getActivity() is fully created in onActivityCreated and instanceOf differentiate it between different Activities if (getActivity() instanceof CallBackListener) callBackListener = (CallBackListener) getActivity(); } @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); Button btn = (Button) view.findViewById(R.id.btn_click); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(callBackListener != null) callBackListener.onCallBack(); } }); } } 

CallbackHandlingActivity.class

 public class CallbackHandlingActivity extends AppCompatActivity implements CallBackListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_all_user); } @Override public void onCallBack() { Toast.makeText(mContext,"onCallback Called",Toast.LENGTH_LONG).show(); } } 
+4
source

Source: https://habr.com/ru/post/944744/


All Articles