我正在尝试从活动类1中的活动类2中激活一个切换按钮。类1中将有一个切换按钮,用于激活位于类2中的另一个切换按钮。我已经了解了共享的首选项,意图和其他内容,但是没有看到任何对我有帮助的代码,或者我只是听不懂。希望有人能指出正确的方向。预先感谢您的帮助。
//活动类2中的切换按钮
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity2);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
location_switch = (MaterialAnimatedSwitch) findViewById(R.id.location_switch);
location_switch.setOnCheckedChangeListener(new MaterialAnimatedSwitch.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(boolean isOnline) {
if (isOnline) {
FirebaseDatabase.getInstance().goOnline(); // set connected when switch to on
if (ActivityCompat.checkSelfPermission(User.this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(User.this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
buildLocationCallBack();
buildLocationRequest();
fusedLocationProviderClient.requestLocationUpdates(mLocationRequest, locationCallback, Looper.myLooper());
//Geo Fire
users = FirebaseDatabase.getInstance().getReference(Common.user_tble).child(Common.currentUser.getType());
geoFire = new GeoFire(users);
displayLocation();
Snackbar.make(mapFragment.getView(), "You are online", Snackbar.LENGTH_SHORT)
.show();
} else {
FirebaseDatabase.getInstance().goOffline(); //Set disconnect when switch to off
fusedLocationProviderClient.removeLocationUpdates(locationCallback);
mCurrent.remove();
mMap.clear();
if (handler != null)
handler.removeCallbacks(drawPathRunnable);
Snackbar.make(mapFragment.getView(), "You are offline", Snackbar.LENGTH_SHORT)
.show();
}
}
});
}
///我可以在活动类1中添加什么以更改活动类2中此切换按钮的状态