betula
betula

Reputation: 1

switching xml layout causes crash in Android

I need to write an android application like a book. I have approximately 100 images and i need to show them with back, forward and another buttons. I tried to create an xml-layout for each image and made images to layout's background.

While running application, if i press buttons fast, program crashes during switching xml-layout.. If i decrease image sizes my problem decreases also. Unfortunately, i need another solution in order to solve it because i cannot use smaller image-size but i have crash problem still.

Upvotes: 0

Views: 156

Answers (2)

Nick
Nick

Reputation: 8317

Part of the problem is that clicking a UI button returns immediately / queues clicks, even though the action associated with that click has not yet completed. For reasons beyond the scope of this response, its worth noting that simply deactivating the button while "doing work" is ineffective. There are a couple solutions to this kind of problem: One is to use a boolean flag that gets set only after the underlying "work" has completed. Then within the button action handler, you ignore button clicks that occur before the flag is reset:

   /**
    * Button presses are ignored unless idle.
    */
   private void onMyButtonClicked() {
      if(idle) {
         doWork();
      }
   }

   /**
    * Does some work and then restores idle state when finished.
    */
   private void doWork() {
      idle = false;
      // maybe you spin off a worker thread or something else.
      // the important thing is that either in that thread's run() or maybe just in the body of
      // this doWork() method, you:
      idle = true;
   } 

Another generic option is to filter using time; ie. you set a limit where the max frequency of button presses is 1hz:

/**
    * Determines whether or not a button press should be acted upon.  Note that this method
    * can be used within any interactive widget's onAction method, not just buttons.  This kind of
    * filtering is necessary due to the way that Android caches button clicks before processing them.
    * See http://code.google.com/p/android/issues/detail?id=20073
    * @param timestamp timestamp of the button press in question
    * @return True if the timing of this button press falls within the specified threshold
    */
   public static synchronized boolean validateButtonPress(long timestamp) {
      long delta = timestamp - lastButtonPress;
      lastButtonPress = timestamp;
      return delta > BUTTON_PRESS_THRESHOLD_MS;
   }

Then you'd do something like this:

private void onMyButtonClicked() {      
      if(validateButtonPress(System.currentTimeMillis())) {
         doWork();
      }
   }

This last solution is admittedly non deterministic, but if you consider that users almost never intentionally click button more than 1-2 times a second on a mobile device, its not so bad.

Upvotes: 0

Raghav Sood
Raghav Sood

Reputation: 82583

Have one layout, with an ImageView in it. Then keep changing the source image for the image view whenever you need to cycle to the next or previous image.

Upvotes: 1

Related Questions