Ale*_*Bcn 6 android google-maps android-emulator
我正在开发一个Android应用程序,在我使用MapsActivity来显示Map的其中一个活动中.我所看到的是,在2.2(API8)模拟器上加载地图需要一些时间,我有时间按下菜单按钮,然后回到应用程序,它仍然在setContentView()上加载,问题当它转到onResume()时会被调用两次.
根据Android活动的生命周期,在onPause() - > [onRestart() - > onStart()] - > onResume()之后,如果应用程序再次到达前台,则会调用onResume()在启动时在onCreate() - > [onStart()]之后调用.
但是如果它仍然在onCreate()的setContentView上加载,为什么不调用一次?
这是我感兴趣的东西,因为每次我使用地图时都不想使用布尔值,认为可以执行两次以避免出现问题,即计数器的双增量.
我不知道这是模拟器的问题,因为我看到的关于横向纵向方向的问题http://code.google.com/p/android/issues/detail?id=2423.
请看看这个:
public class LocationActivity extends MapActivity {
private static final String TAG = "LocationActivity";
private static int i;
protected void onCreate(Bundle savedInstanceState) {
i = 0;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_location);
}
protected void onResume(){
super.onResume();
i++;
Log.w(TAG,String.valueOf(i));
showDialogSettings();
}
private void showDialogSettings() {
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
String title = "I-Value:" + String.valueOf(i);
String positiveButton = "OK";
final Intent intent = new Intent(Settings.ACTION_SETTINGS);
dialog.setTitle(title);
dialog.setPositiveButton(positiveButton, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent settingsIntent = intent;
startActivity(settingsIntent);
}
});
dialog.show();
}
@Override
protected boolean isRouteDisplayed() {
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
activity_location.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.google.android.maps.MapView
android:id="@+id/locationactivity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:apiKey="XXXXXXXXXXXXXXX"
android:clickable="false"
android:enabled="true" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
您可以重新创建问题:
阅读Geobits和G. Blake Meike所做的评论,这部分是一个答案,只是为了澄清我是否错了.
也许由于地图的异步负载,地图的例子很糟糕.我更改了一个Activity的MapsActivity,让我们假设手机过载了,所以onCreate会执行一个8秒的循环(不是Android Not Responding的时间).
这里使用轻量级Android布局的新代码:
public class LocationActivity extends Activity {
private static final String TAG = "LocationActivity";
private static int i;
protected void onCreate(Bundle savedInstanceState) {
i = 0;
Log.w(TAG,"onCreate");
super.onCreate(savedInstanceState);
setContentView(android.R.layout.simple_spinner_item);
Log.w(TAG,"beforeLoop");
try {
for(int j = 0; j < 8; j++){
Log.w(TAG,"Sleeping...");
Thread.sleep(1000);
}
} catch (InterruptedException e) {
}
Log.w(TAG,"afterLoop");
}
protected void onResume(){
super.onResume();
Log.w(TAG,"onResume" + String.valueOf(i));
i++;
Log.w(TAG,"check Mobile Connectivity");
ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if(networkInfo != null && networkInfo.getType() == ConnectivityManager.TYPE_MOBILE && networkInfo.isAvailable()){
Toast.makeText(this, "Network available!", Toast.LENGTH_LONG).show();
Log.w(TAG,"showingToast");
}
}
protected void onPause(){
super.onPause();
Log.w(TAG,"onPause:" + String.valueOf(i));
}
protected void onStop(){
super.onResume();
Log.w(TAG,"onStop:" + String.valueOf(i));
}
}
Run Code Online (Sandbox Code Playgroud)
如果我在日志仍在打印"睡觉..."时最小化应用程序并且很快我重新运行应用程序我可以看到"正在睡觉...",onResume将检查两次连接(这是一种正确的方式检查网络连接).
这里是logCat:
Toast将显示两次消息.
查看logCat生命周期是正确的,但我只想考虑到有时onCreate可能因为系统过载而延迟,如果onResume被执行两次,那么我必须处理一些初始化,所以我必须使用布尔值我认为我不应该使用,因为onCreate仍在运行.
如果不是Toast它是一个对话框,则从用户的POV中不欢迎两个对话框.
请以与我相同的方式执行代码,我很顽固,不能放弃:P
归档时间: |
|
查看次数: |
10040 次 |
最近记录: |