我的问题如下:
当我点击ActionBar中的按钮时,它会进入错误的活动状态。当我处于错误的活动并按下后退按钮时。这是它应该去的那个。
当按下按钮时,它应该转到AddActivity。
如果要求任何其他代码,请询问。
MapsActivity
public class MapsActivity extends AppCompatActivity implements OnMapReadyCallback,
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
LocationListener {
private GoogleMap mMap;
private GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRequest;
private Location mLastLocation;
private Marker mCurrLocationMarker;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
checkLocationPermission();
}
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
initialize();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_add_marker:
// User chose the "Add marker" item
Intent addActivityIntent = new Intent(MapsActivity.this, AddActivity.class);
startActivity(addActivityIntent);
case R.id.action_logout:
// User chose the "Uitloggen" item
PokeMapperModel.getInstance().setCurrentUser(null);
Intent loginActivityIntent = new Intent(MapsActivity.this, LoginActivity.class);
startActivity(loginActivityIntent);
finish();
default:
// Action not recognized
return super.onOptionsItemSelected(item);
}
}
private boolean isUserLoggedIn() {
return PokeMapperModel.getInstance().getCurrentUser() != null;
}
private void initialize() {
if (!isUserLoggedIn()) {
Intent intent = new Intent(MapsActivity.this, LoginActivity.class);
startActivity(intent);
finish();
}
}
答案 0 :(得分:5)
您忘记在每个switch语句后添加break;
。
编辑:您不需要添加default:
,请查看更新后的代码。
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_add_marker:
// User chose the "Add marker" item
Intent addActivityIntent = new Intent(MapsActivity.this, AddActivity.class);
startActivity(addActivityIntent);
break;
case R.id.action_logout:
// User chose the "Uitloggen" item
PokeMapperModel.getInstance().setCurrentUser(null);
Intent loginActivityIntent = new Intent(MapsActivity.this, LoginActivity.class);
startActivity(loginActivityIntent);
finish();
break;
}
return super.onOptionsItemSelected(item);
}
答案 1 :(得分:1)
您需要return true;
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.one:
one();
return true;
case R.id.two:
two();
return true;
default:
return super.onOptionsItemSelected(item);
}
}