-1

I have a classic example of Preferences where I want to call MainActivity method to hide ImageView.

But it does not go to debug even.

So how to call method of MainActivity properly?

public class SettingsActivity extends AppCompatPreferenceActivity 
...

@Override
        public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
            try {
                if (key.equals("isGPS_Switch")) {
                    Boolean isGPSSwitch = sharedPreferences.getBoolean(key, false);
                    if (isGPSSwitch) {
                        // START SERVICE
                        getActivity().startService(new Intent(getActivity(), gpsService.class));

                        ((MainActivity)getActivity()).MyLocationMapDisplay(true);

                    } else {
                        // STOP SERVICE
                        getActivity().stopService(new Intent(getActivity(), gpsService.class));

                        ((MainActivity)getActivity()).MyLocationMapDisplay(false);

                    }
                }
            } catch (Exception ex) {
                Log.e("Preferences", ex.getMessage());
            }
        }

And MAinActivity class

public class MainActivity extends AppCompatActivity 
...
 private ImageView imageDisplayGPS;

 public void MyLocationMapDisplay(boolean isDisplay)
    {

        if(isDisplay)
        {
            imageDisplayGPS.setVisibility(View.VISIBLE);
        }
        else
        {
            imageDisplayGPS.setVisibility(View.GONE);
        }
    }
NoWar
  • 36,338
  • 80
  • 323
  • 498
  • 2
    It is not a good idea in Android to reference one Activity from another. They have independent life cycles and trying to access one from the other brings in all kinds of trouble. – Henry Feb 08 '18 at 06:23
  • @Henry Ok. Would u mind to provide correct solution, please? – NoWar Feb 08 '18 at 06:23
  • And even if ^that wasn't true, it would be best to avoid coupling your code together like that. – ChiefTwoPencils Feb 08 '18 at 06:24
  • @ChiefTwoPencils Great! Please provide correct solution. Thanks! – NoWar Feb 08 '18 at 06:26
  • One way would be to check the preference setting in `MainActivity` to decide if the UI should be shown or not. – Henry Feb 08 '18 at 06:27
  • @Yeah... But It is a Launcher app. So have I restart it somehow? – NoWar Feb 08 '18 at 06:30
  • Is Homeactivity is the launcher screen of your app ?? – Yashaswi N P Feb 08 '18 at 06:32
  • `PreferenceActivity` have access to a `SharedPreferences`. That is how you should read/share a variable across activities. https://stackoverflow.com/questions/3624280/how-to-use-sharedpreferences-in-android-to-store-fetch-and-edit-values – OneCricketeer Feb 08 '18 at 06:33
  • Also https://stackoverflow.com/questions/2614719/how-do-i-get-the-sharedpreferences-from-a-preferenceactivity-in-android – OneCricketeer Feb 08 '18 at 06:36
  • @YashaswiNP Yep! And it works fine. :) – NoWar Feb 08 '18 at 06:38

1 Answers1

0

Nothing could simple than using BroadcastReciver

private void configureBroadcastReciver() {
        broadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent i)
            {
                try
                {
                    String imageDisplayGPSState = (String) i.getExtras().get("btn_imageDisplayGPS");
                    if(imageDisplayGPSState.equals("true"))
                    {
                        MyLocationMapDisplay(true);
                    }
                    else
                    {
                        MyLocationMapDisplay(false);
                    }
                }
                catch (Exception ex)
                {
                    Log.d("MAIN ACTIVITY", ex.getMessage());
                }
            }
        };

        registerReceiver(broadcastReceiver, new IntentFilter("buttons_visible"));
    }

And in the Preferences is

@Override
        public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
            try {
                if (key.equals("isGPS_Switch")) {
                    Boolean isGPSSwitch = sharedPreferences.getBoolean(key, false);
                    if (isGPSSwitch) {
                        // START SERVICE
                        getActivity().startService(new Intent(getActivity(), gpsService.class));

                        Intent buttons_visible = new Intent("buttons_visible");
                        buttons_visible.putExtra("btn_imageDisplayGPS", "true");
                        getContext().sendBroadcast(buttons_visible);


                    } else {
                        // STOP SERVICE
                        getActivity().stopService(new Intent(getActivity(), gpsService.class));

                        Intent buttons_visible = new Intent("buttons_visible");
                        buttons_visible.putExtra("btn_imageDisplayGPS", "false");
                        getContext().sendBroadcast(buttons_visible);


                    }
                }
            } catch (Exception ex) {
                Log.e("Preferences", ex.getMessage());
            }
        }
    }
NoWar
  • 36,338
  • 80
  • 323
  • 498