There are 2 buttons A and B, when you click on A., a certain b.OnClickListener is launched.

What are the consequences of this approach? Leaks did not notice ....


Button button=new Button(this); button.setOnClickListener(v -> { mapFragment.getMapAsync(googleMap -> { System.out.println("TEST"); }); }); 

@pavlofff , you probably did not understand what I want to know). I don’t know about GC. But I am studying MVP now and have encountered a problem when using Google Maps. I found a theme in SO like my GoogleMap MVP task. I decided to try the code. The code is working, but it is not clear how it works. When you rotate the screen. Throws a NullPointer error in addMarkersOnMap .

I solved this problem and moved mapFragment.getMapAsync(googleMap -> to addMarkersOnMap . When you turn, everything works well and nothing is caused. Only the data is added to the map.

The addMarkersOnMap method addMarkersOnMap called several times. It can be called 10 times with an interval of 2 seconds, for example. Therefore, I want to know if it is right to do so. If not, what alternative way to find, to solve my problem.

  • 3
    Show me an example. - post_zeew
  • @post_zeew, added an example - Andro
  • one
    Best of all, at the time of the asynchronous operation, lock the button. At a minimum, this is good in terms of UI / UX. - post_zeew
  • one
    once an assigned listener will “act” until the object he “listens to” is destroyed, so there is no point in appointing several times. When you re-appointment will be simply performed useless operation. Objects that were created during the previous assignment are likely to lose references to the object being listened to and will be destroyed by the GC at an opportunity. All this will create an additional load on the system and nothing else. - pavlofff
  • You can assign a listener once, when the button is initialized, and if you need dynamic actions from clicking on it, then call the method from the listener, which will "resolve" the dynamics. - pavlofff

1 answer 1

Obviously, pressing will be processed every time. The consequence of this may be a working button.