0

I'm trying to send a broadcast from Setting Fragment and listen to it in another activity. I started settings activity with startActivityForResult from ActivityA. SettingsActivity has a static fragment SettingsFragment:

public static class SettingsFragment extends PreferenceFragment {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(R.xml.pref_general);
            Preference rateApp = findPreference("rate_this_app");

            Preference abcd = findPreference("abcd");
            rateApp.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
                @Override
                public boolean onPreferenceClick(Preference preference) {

                }
            });




        abcd.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
            @Override
            public boolean onPreferenceClick(Preference preference) {
                Intent intent = new Intent(getActivity(), ActivityA.class);
                intent.setAction("abcd");
                getActivity().sendBroadcast(intent);
            }
        });
    }
}

The broadcast sent from abcd.setOnPreferenceClickListener is registered in ActivityA but it is not being received there for some reason. Is it because the activity is in backstack that it won't receive broadcast?

crysis
  • 1,514
  • 1
  • 20
  • 35
  • @jankigadhiya No. I know how to send receive broadcast but this one not working for some reason. – crysis Jun 20 '16 at 04:39
  • If your Activity is paused it won't receive. http://stackoverflow.com/questions/7890363/broadcastreceiver-and-paused-activity – Gokhan Arik Jun 20 '16 at 04:45
  • Try to debug, check that Activity A's onDestory is already called or not ? and show Activity A code for better understanding – Khizar Hayat Jun 20 '16 at 04:46
  • @GokhanArik That seems to be my case. Any alternative for this? – crysis Jun 20 '16 at 04:52
  • Check My Answer http://stackoverflow.com/questions/36521019/should-broadcastreceiver-be-declared-inside-activities/36521205#36521205] – Raghavendra B Jun 20 '16 at 05:19
  • How about a code snippet from activity A including the zone that you register your receiver with a suitable IntentFilter? That would make the question clearer and solutions to be provided faster. Also if your activity A is on pause, try Intent.FLAG_ACTIVITY_REORDER_TO_FRONT and start your activity instead of sending broadcast – Burak Day Jun 20 '16 at 05:28
  • Check http://stackoverflow.com/a/37522152/4994743 – Burak Day Jun 20 '16 at 05:33

1 Answers1

0

If your Activity is in paused state you won't receive broadcast.

Similar question

An alternative to that depends on your goal. If your goal is to change some settings in the app, I would save it in SharedPreferences as soon as the user make changes. In ActiivityA's onCreate method I would read it from SharedPreferences. BroadcastReceivers are used to notify developer if there is an action needs to be taken. For example, if a change in setting will change views on user's screen, BroadcastReceiver is useful. You would get notified and update your UI. If the page is not visible to the user, you wouldn't need that.

Community
  • 1
  • 1
Gokhan Arik
  • 2,626
  • 2
  • 24
  • 50