I'm developing an app that implements mapsView. I have two activities that lead to the mapsView activity. activity_A will lead to mapsView activity, and the mapsView activity will lead to the activity_B. I'm implementing putExtra()
and getBooleanExtra()
in my code.
Here's my code in activity_A
case R.id.buttonMaps:
Intent i = new Intent(MainActivity.this, MapsActivity.class);
i.putExtra("maps", false);
startActivity(i);
break;
Here's my code in activity_B
buttonNavigasi.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(DetailActivity.this,MapsActivity.class);
i.putExtra("nama", daftarNama);
i.putExtra("deskripsi", daftarDeskripsi);
i.putExtra("foto", daftarFoto);
i.putExtra("marker", daftarMarker);
i.putExtra("lat", daftarLat);
i.putExtra("lng", daftarLng);
i.putExtra("maps", true);
startActivity(i);
}
});
Here's my code in mapsView activity
case R.id.action_refresh:
removeAllMarkers();
if(i.getBooleanExtra("maps", true)) {
mMyMarkersArray.add(new MyMarker(i.getStringExtra("nama"), i.getStringExtra("deskripsi"), i.getStringExtra("foto"), i.getStringExtra("marker"), Double.parseDouble(i.getStringExtra("lat")), Double.parseDouble(i.getStringExtra("lng"))));
UI_HANDLER.postDelayed(UI_UPDATE_RUNNABLE, 500);
}
else {
registerReceiver(mNetworkReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
}
getCurrentLocation();
return true;
The problem is when the mapsView go to activity_B and in activity_B I pressed the back button (back to mapsView activity) then I pressed the refresh icon in mapsView, the mapsView will go to this code
if(i.getBooleanExtra("maps", true)) {
mMyMarkersArray.add(new MyMarker(i.getStringExtra("nama"), i.getStringExtra("deskripsi"), i.getStringExtra("foto"), i.getStringExtra("marker"), Double.parseDouble(i.getStringExtra("lat")), Double.parseDouble(i.getStringExtra("lng"))));
UI_HANDLER.postDelayed(UI_UPDATE_RUNNABLE, 500);
}
Why it doesn't go to the else
statement in mapsView activity? I do not press the buttonNavigasi
in activity_B, but I just press the back button. What's wrong with my code?
Any answer will be greatly appreciated.
Thanks in advance.
if(i.getBooleanExtra("maps", true)) {
Here you are telling it to get
maps
, if it doesn't exist it will simply be true.If you set it to false, you will get the correct answer.
Because when you do not assign any value he get by default true
i.getBooleanExtra("maps", true)
try withi.getBooleanExtra("maps", false)
:EDIT
Following the life cycle of an activity when you press back button maybe this solution in
MapsActivity
could be more useful for you:Hope help
When you press back button your previous activity is just resumed
you could override your own onBackPressed
to force the user to navigate through your buttons or to pass through the onBackPressed the value of the boolean that you want
or you could simply
finish()
each activity after using thestartActivity()
so you won't use too much memory in the same time avoid those loops that way you can only start the map through the intent