简体   繁体   English

onSizeChanged()调用onCreate()和onStart()吗? -安卓

[英]onSizeChanged() calls onCreate() and onStart()? - Android

My app currently calls a method within the onCreate() method so that the game will start and animations will run once the view is created. 我的应用程序当前在onCreate()方法中调用一个方法,这样,一旦创建视图,游戏便会开始并且动画将运行。 However when i flip the screen to switch between portrait and landscape, this method is called again. 但是,当我翻转屏幕以在纵向和横向之间切换时,将再次调用此方法。

I've moved the calling line both to the onStart() method and even the methods class constructor. 我已经将调用行移到了onStart()方法甚至方法类构造函数上。

this is the method that is being called: 这是被调用的方法:

public void startGame() {
    Handler handler = new Handler();
    handler.postDelayed(new Runnable() {

        public void run() {
            runGame();
        }
    }, 500);
}

There is a delay to allow everything to be constructed before it is run, otherwise it won't work. 延迟所有内容的构造,然后再运行,否则将无法正常工作。

Is there i way to stop onSizeChanged() affecting this method being called? 我有办法停止onSizeChanged()影响此方法的调用吗? Or is there a way i can call this method so that it starts when the activity is started (again so that onSizeChanged() cant affect it and that everything is initialized before its call). 还是有一种方法可以调用该方法,以便在活动开始时启动该方法(再次使onSizeChanged()不能影响该方法,并且在调用之前将所有内容初始化)。

Thanks for looking. 感谢您的光临。

The easiest way is to add: 最简单的方法是添加:

android:configChanges="orientation|screenSize"

which tells the system you'll take care of them yourself. 告诉系统您自己来照顾他们。 However, it's not recommended. 但是,不建议这样做。

Note: Using this attribute should be avoided and used only as a last-resort. 注意:应避免使用此属性,并且只能将其用作最后一种手段。 Please read Handling Runtime Changes for more information about how to properly handle a restart due to a configuration change. 请阅读处理运行时更改以获取有关如何正确处理由于配置更改而导致的重新启动的更多信息。

Or you can check savedInstanceState. 或者,您可以检查savedInstanceState。 If it is not null, that means a user changes the orientation (or the app are coming back from a configuration change). 如果不为null,则表示用户更改了方向(或应用程序因配置更改而返回)。 Maybe something likes this: 也许是这样的:

if(savedInstanceState == null)
    startGame();
else
    //handle a configuration change if necessary

Please see Don't reload application when orientation changes 请参阅方向更改时不要重新加载应用程序

Edit : 编辑:

if(savedInstanceState == null) {
    startGame();
} else {
    //handle a configuration change if necessary
    int yourVar = savedInstanceState.getInt("something");
}

将此添加到您的清单:

android:configChanges="screenSize"

Could you use a static thread variable instead of handler? 您可以使用静态线程变量代替处理程序吗?

eg 例如

  private static Thread gameThread = new Thread(new Runnable(){
        public void run()
        {
              //if necessary: Thread.sleep(500);
              runGame();
        }
  }

  @Override
  protected void onCreate(Bundle savedInstanceState) {
        //rest of oncreate code here
        if(gameThread.isAlive() == false)
        {
              gameThread.start();
        }
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM