Disposing Interface Implementation On Fragment Destroy

Viewed 14

I have a background service that broadcasts to MainActivity the internet connection status.

private BroadcastReceiver mReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(mBroadcastBooleanAction)) {
            boolean isConnectedToInternet = intent.getBooleanExtra("Data", false);
            if (internetConnectionInterface == null) return;
            internetConnectionInterface.connectedToInternet(isConnectedToInternet);
        }
    }
};

Then on the MainActivity, I have a navGraph that navigates between fragments by using Navigation.findNavController.

I created an interface in the MainActivity to pass data from the MainActivity to its fragments

public class MainActivity extends BaseActivity{
    public interface InternetConnectionInterface {
        void connectedToInternet(boolean isConnected);
    }
}

Then when I navigate to a different fragment, I implement this interface like so

public class MainMenuFragment extends Fragment 
    implements MainActivity.InternetConnectionInterface {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        ((MainActivity)getActivity()).setInternetConnectionInterface(this);
        OnBackPressedCallback callback = new OnBackPressedCallback(true) {
            @Override
            public void handleOnBackPressed() {
                AlertDialog.Builder builder= new AlertDialog.Builder(getContext());
                builder.setMessage("Are you sure you want to exit the Entry Transaction Module?");
                builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        try {
                            requireActivity().onBackPressed();
                        } catch (Exception e) {
                            showErrorAlert("Go back", e.getMessage());
                        }
                    }
                });
                builder.setNegativeButton("No", null);
                builder.show();
            }
        };
        requireActivity().getOnBackPressedDispatcher().addCallback(this, callback);
    }
    
    ....
    
}

public class EntryFragment extends Fragment 
    implements MainActivity.InternetConnectionInterface { 
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        ((MainActivity)getActivity()).setInternetConnectionInterface(this);
        OnBackPressedCallback callback = new OnBackPressedCallback(true) {
            @Override
            public void handleOnBackPressed() {
                AlertDialog.Builder builder= new AlertDialog.Builder(getContext());
                builder.setMessage("Are you sure you want to exit the Entry Transaction Module?");
                builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        try {
                            requireActivity().onBackPressed();
                        } catch (Exception e) {
                            showErrorAlert("Go back", e.getMessage());
                        }
                    }
                });
                builder.setNegativeButton("No", null);
                builder.show();
            }
        };
        requireActivity().getOnBackPressedDispatcher().addCallback(this, callback);
    }
    
    ...
    
}

Both of these fragments overrides the interface as so

@Override
public void connectedToInternet(boolean isConnected) {
    Log.i("LOGGER", "THIS IS CALLED");
}

and both are running fine. My problem is when I go back to the MainMenuFragment, the implementation of the interface in the EntryFragment still functions.

0 Answers
Related