小编Ale*_*Bcn的帖子

如果应用程序仍在运行onCreate方法,为什么onResume方法将被执行两次?

我正在开发一个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 …
Run Code Online (Sandbox Code Playgroud)

android google-maps android-emulator

6
推荐指数
1
解决办法
1万
查看次数

标签 统计

android ×1

android-emulator ×1

google-maps ×1