App is minimized during test

Hi ,
i created a simple appium code (eclipse + java)
that open gallery -> selecting from a scroll view item (video file) in position 0 ,
play the video , stop it after 5 seconds.
the selecting item in position 1 -> play it…and so on…
the test is passing around 70% of the times.
but at the other 30% when it tries to get item in position (0 or 1)
the app is minimized by itself…and then it cannot find the element and test is failed.
any ideas ? known issues ?
here is the part of my code that failes sometimes:
try {
Thread.sleep(2000);
WebElement galleryScrollView = driver.findElement(By.id(“com.mv4d.oxalis:id/scrollView1”));
List galleryChildElements = galleryScrollView.findElements(By.className(“android.widget.ImageView”));
System.out.println("There are " +galleryChildElements.size()+ " items in the gallery. ");
Thread.sleep(2000);
System.out.println("Clicking on item in postion " +0);
gallerySelectedElement = galleryChildElements.get(0);
gallerySelectedElement.click();
Thread.sleep(6000);
System.out.println("File has been loaded properly ");
} catch (Exception e) {
System.out.println(“Fail to load file from the gallery … No Element found”);
}