Из основного класса выполняю в методе onCreate 'setContentView(R.layout.activity_main);' 

In activity_main I connect a class for drawing the playing field + some calculations:

  <com.example.sapeg.myapplication.MyDraw android:id="@+id/view" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="7"/> 

Question: why does the JVM perform twice the calculations that are in the class MyDraw (twice calls the onDraw method). This is the first pass further called once per request. Though I wrote "multithreading", but through LogCat it turned out that all this is done in one stream "main".

  • 2
    The onDraw method onDraw called every time the system considers it necessary to redraw the object, and the extra calculations in this method are already on your conscience. - woesss
  • Is it possible to track the places where the system calls onDraw ? - sapeg
  • one
    What for? In onDraw you have to draw your content into the provided Canvas and that’s all you need from it. It is called when an event occurs that affects your view (appearance on the screen, click, shift, sneeze, puff)), as well as at your request: view.invalidate() - woesss
  • So I draw content there. I deduce a figure in a random-place. But since onDraw runs twice, the shape moves from the place I need. - sapeg
  • one
    Take out the calculation of the location to another method, remember the result of the calculation in the class fields, and in onDraw take the values ​​from these fields. Call the calculation according to the logic of the change of location, and not from onDraw . Then it will not depend on his calls. For example, the initial position can be calculated in the constructor, and then by clicking the button or how you should have it. - woesss

0