so I'm having a ViewPager with three fragments. In the middle one, I'd like to set a TextView to Visible, if GPS is deactivated, and hide it, if it's activated. To react on changes on the GPS module's availability, I'm doing something like this:
@Override
public void onProviderDisabled(String provider) {
if (provider.equals(LocationManager.GPS_PROVIDER)) {
// gps deactivated, sucks dude
GPS_ACTIVE = false;
((Nearby) mTabsAdapter.getItem(1)).notifyGpsDeactivated();
}
}
@Override
public void onProviderEnabled(String provider) {
if (provider.equals(LocationManager.GPS_PROVIDER)) {
// gps activated, start again!
GPS_ACTIVE = true;
((Nearby) mTabsAdapter.getItem(1)).notifyGpsActivated();
}
}
In the "Nearby" Fragment, it looks like this:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_nearby, null, false);
int VISIBLE = (SHOW_GPS_NOTIFICATION) ? View.VISIBLE : View.GONE;
Log.d("Nearby", "Show gps not? " + SHOW_GPS_NOTIFICATION);
view.findViewById(R.id.nearby_notification_activategps).setVisibility(
VISIBLE);
return view;
}
public void notifyGpsActivated() {
SHOW_GPS_NOTIFICATION = false;
if (view != null)
view.findViewById(R.id.nearby_notification_activategps)
.setVisibility(View.VISIBLE);
else
Log.d("Nearby", "View is null :(");
}
public void notifyGpsDeactivated() {
Log.d("Nearby", "Gps deactivated.");
SHOW_GPS_NOTIFICATION = true;
if (view != null)
view.findViewById(R.id.nearby_notification_activategps)
.setVisibility(View.VISIBLE);
else
Log.d("Nearby", "View is null :(");
}
- View is a private variable in the Nearby class, I'm saving the reference to the view in onCreateView, to use it later on in the notification methods, but view is always null. I even tried in the fragment's method to obtain the view with "getView()", but it was null also. Anyone has an advice, how to solve this problem? Also, the "onProviderEnabled()" seems not to work if I activate GPS while the application is paused - should I check for available GPS providers in onResume as well?
Thanks!
Oh yeah btw, I set both values to VISIBLE because it's never visible anyway, just for testing purposes - I NEVER see the textview.. so that's not it. :)