I have an android application where I always send data automatically when moving it to the foreground. An image either chosen from gallery or shot with the camera itself can be part of the data. When chosen by the gallery, my app doesn't go into the background and everything works fine. But when I shoot an image with the camera, the app goes into the background and when it resumes it instantly sends the data without the image because it wasn't processed yet in that time.
So now I want to set a flag whenever the camera is launched and my application is sent to the background because of that, so that I can check like this e.g.
@OnLifecycleEvent(Lifecycle.Event.ON_START)
public void onMoveToForeground() {
if (SomeSingleton.getInstance().getFlag() != 1) {
sendData()
} else {
SomeSingleton.getInstance().setFlag = -1;
}
}
But to set that flag I first need to know the reason for the onStop(), to make sure the app didn't get stopped for something else. I want to do something like this then:
@Override
public final void onStop() {
if (cameraInUse()) {
SomeSingleton.getInstance().setFlag(1);
}
super.onStop();
}
Is this somehow possible and if yes how?
The library you are using has the option to add your own click listeners for camera and gallery (using IPickClick
).
As per to documentation at https://github.com/jrvansuita/PickImage#additionals you can do something like this
PickImageDialog.build(setup)
.setOnClick(new IPickClick() {
@Override
public void onGalleryClick() {
SomeSingleton.getInstance().setFlag(0); // use your code to set flag for gallery
}
@Override
public void onCameraClick() {
SomeSingleton.getInstance().setFlag(1); // use your code to set flag for camera
}
}).show(this);
Now you can use this flag to check if your app went in the background on camera or gallery click.