问题描述
我正在开发实现mapsView的应用程序.我有两个活动导致了mapsView活动.activity_A将导致mapsView活动,而mapsView活动将导致activity_B.我正在我的代码中实现 putExtra()
和 getBooleanExtra()
.
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.
这是我在activity_A中的代码
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;
这是我在activity_B中的代码
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);
}
});
这是我在mapsView活动中的代码
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;
问题是,当mapsView转到activity_B并在activity_B中按下返回按钮(返回mapsView活动),然后在mapsView中按下刷新图标时,mapsView将转到此代码
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);
}
为什么在mapsView活动中没有转到 else
语句?我不按activity_B中的 buttonNavigasi
,而是按返回按钮.我的代码有什么问题?
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?
任何答案将不胜感激.
谢谢.
推荐答案
当您按下后退"按钮时,您先前的活动将恢复.
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;
这篇关于Android中的putExtra()和getBooleanExtra()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!