0

I am trying to call a function in a OnPreferenceClickListener which is defined in a another class. Since I have not managed to initialisation an interface in OnPreferenceClickListener. I have given an example code below:

public void onCreatePreferences(Bundle bundle, String s) {

    ListPreference preference = findPreference(getString(R.string.settings_ble_choose_device_key));

    preference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
        @Override
        public boolean onPreferenceClick(@NonNull Preference preference) {

            callFunctionInMainActivity();

            return false;
        }
    });
}

How i can call a function witch is implement in a another class?

Thank you very much

Rene

Jens
  • 67,715
  • 15
  • 98
  • 113
rene
  • 99
  • 7

1 Answers1

1

You can implement an intent for this. Where you send a broadcast from your OnPreferenceClickListener class and implement a broadcast received in the other class to listen for this intent and invoke the method that you want. Here is an example:

public void onCreatePreferences(Bundle bundle, String s) {

    ListPreference preference = findPreference(getString(R.string.settings_ble_choose_device_key));

    preference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
        @Override
        public boolean onPreferenceClick(@NonNull Preference preference) {

                        sendBroadcast(new Intent(Constants.ACTION_STOP_MAIN_SERVICE));


            return true;
        }
    });
}

In your other class:

private final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {

        String action= intent.getAction();
        if(action.equalsIgnoreCase(ConstantesIdentifiant.ACTION_STOP_MAIN_SERVICE)){
            finishAffinity();
        }
    }
};


@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        registerReceiver(broadcastReceiver, new IntentFilter(ConstantesIdentifiant.ACTION_STOP_MAIN_SERVICE));
}

@Override
protected void onDestroy() {
    unbindService(mConnection);
    unregisterReceiver(broadcastReceiver);
    super.onDestroy();
}
Charles Semaan
  • 304
  • 2
  • 13
  • Does a broatcastreseiver also make sense if I want to call a function in another class and wait for a response. In my case I would like to write all BLEDevices in a string array and then pass them to the ListPreference. – rene Feb 21 '23 at 16:03
  • yes you can create an intent to request the data which will invoke the function when it received. Then in your function when you invoke it you create another intent when it finishes and add the data to the intent itself here is a [link](https://stackoverflow.com/questions/8610880/how-do-i-create-an-android-intent-that-carries-data) on how to add data to an intent. Note that you will have to implement receiver on both classes this way. There might be a cleaner way but this will also work. – Charles Semaan Feb 22 '23 at 09:21