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.
When you press back button your previous activity is just resumed
private boolean registerRecr=false;
@Override
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
registerRecr=true;
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
}
case R.id.action_refresh:
removeAllMarkers();
if(i.getBooleanExtra("maps", true)&& registerRecr==false) {
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;
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 with i.getBooleanExtra("maps", false)
:
case R.id.action_refresh:
removeAllMarkers();
if(i.getBooleanExtra("maps", false)) {
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;
EDIT
Following the life cycle of an activity when you press back button maybe this solution in MapsActivity
could be more useful for you:
Intent i;
boolean refresh=false;
//YOUR CODE
@Override
protected void onCreate(Bundle savedInstanceState) {
//YOUR CODE
i = getIntent();
//YOUR CODE
}
@Override
protected void onResume() {
//YOUR CODE
if(i!=null){
refresh = i.getBooleanExtra("maps", false);
}
//YOUR CODE
}
@Override
protected void onPause() {
//YOUR CODE
i=null;
//YOUR CODE
}
//YOUR CODE
case R.id.action_refresh:
removeAllMarkers();
if(refresh) {
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;
//YOUR CODE
Hope help
you could override your own onBackPressed
@Override
public void 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 the startActivity()
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