Ionic App `appRestoredResult` Doesn't Fire - android

I am building an Ionic app that uses the camera plugin, and I am having problems with getting the data to come back from the camera. As the Capacitor docs on the Camera plugin (In the paragraph above the variables section) say, the app will sometimes be "terminated" in the background and so when it returns to the app, you need to listen for the "appRestoredResult in the App plugin" to get the camera result. The problem that I am having is that this event never seems to fire.
I added listeners for the following events: backButton, pause, resume, appRestoredResult, and appStateChange.
backButton works, and shows the toast notification (which is what I was using to test to see if the events were fired).
pause seems to only be fired if I press the "home" or "opened apps" button (the ones on the phone, not part of my app).
resume only fires when I return to the app after the pause event successfully fired. This event should also fire when I come back from the camera as well, shouldn't it?
appRestoredResult doesn't seem to fire ever. Which it should fire after returning to the app after the camera (if the app was terminated during the picture taking).
appStateChange fires when I pause or resume the app.
What am I doing wrong?

With a little more digging and a little more testing, I figured out the problem.
The reason why appRestoredResult event isn't firing, is because it only works with a specific camera library (which I had switched to previous to see if I could get that to work, but then I started using these events, and didn't switch back).
This is what I had previously
...
import { App } from "#capacitor/app";
import { Camera } from "#ionic-native/camera/ngx";
...
This is what I have now
...
import { App } from "#capacitor/app";
import { Camera } from "#capacitor/camera";
...
So if you are having trouble with the appRestoredResult event not firing, check your libraries and make sure you are using both from #capacitor.

Related

Navigation 5 'focus' listener not triggered when app is brought back up from background

I am using React Navigation 5, and I am trying to set up a focus listener on a Screen to perform some actions.
The Screen where I need to set up the listener is a Class component that triggers the browser launch, and I want to listen to when the user closes the browser with the "back navigation" and return to the app.
While testing this flow I noticed that the listener is not triggered when returning from the browser, or even when coming up from the background. On the other hand, I could successfully test that when navigating between screens within the app everything works as expected.
Moreover, I set up also the unsubscriber for the focus listener and I can see that it's not called improperly, and the listener still seems alive.
I searched for other issues, questions on SO, read the docs, but I couldn't find a solution. It's also true that I'm a newbie as long as React Native is concerned, but I would expect that the focus listener is triggered also after putting the app to background.
Or is there actually a way to achieve that?
I managed to create a Snack with a sample code (my app is not using Expo CLI, but I saw that the issue is correctly reproduced there too).
It's a very simple StackNavigator with 2 screens. Screen1 is the Class component in which I want to listen for the focus.
by pressing the "GO TO BROWSER" button, the Google website is opened, but navigating back to the app no logs appear informing that the focus is on;
going to background and getting the app back up no logs appear for the focus;
by pressing the "GO TO SCREEN 2" button, the second screen is opened, and upon back navigation, the focus test log informs us that the focus is now on.
My software versions:
iOS and Android 12.4.7 and API 29&24
#react-navigation/native 5.5.1
#react-navigation/stack 5.5.1
react-native-gesture-handler 1.6.1
react-native-safe-area-context 3.0.5
react-native-screens 2.8.0
react-native 0.62.2
node 12.17.0
npm 6.14.6

Close app when minimized

I have similar problem like this.
On start my app displays a splash screen and checks via network if the current user is still premium.
My problem: I started my app right before I went to bed and minimized it by pressing the home button. In the morning I launched the app again and it resumed the activity from the night. The app never really quit, my splash screen was not shown and and it couldn't check if the user is still premium.
So how can I achieve my app to be closed after a certain time (e.g. when the app is minimized)?
But the problem is there is a portion in the app where I can view videos in full screen and here I use android's default player. So when the app is minimized while watching and then again open the app onResume will not be called and cannot check whether it is a registered user or not. The video player will continue to play the video. Is there any method so that I can kill the app when the video is playing and minimised?? is there any method which is called when the app is minimised using home key press?? Is it possible in every device to detect the home keypress event and write some code there?? Please help with some fresh ideas!!!
Now I know why my reputation is always low. Thanks Dan Hulme. You are right I just want to use the app lifecycle correctly. I want to use onRestart not onResume. And that have done the work for me. Thanks all.

Cordova - detect that app is in the background

I have a feature in my cordova app that lets user do something when user shakes the phone (I'm using shake.js for the purpose). The phone vibrates when it happens.
The problem is that, when I "alt tab" away from the app and it's in the background, the shake/vibrate combo still works. So the user might be using a completely different app and the phone would still vibrate.
Is there a way to detect that the app is in the background, or better yet, freeze the app somehow when it's going to the background?
I have this problem on Android (haven't tested it on iOS yet).
You can simply bind the pause event and you remove the shake feature from your app.
According to the documentation The (pause) event fires when an application is put into the background.
You can listen for the resume event to put back the shake feature.
As usual in the documentation you can find complete code examples.

Adobe Flash Builder Flex Mobile Android: Avoid that user can close the app

I am developing an app for android. It is not always clear to me when the app will be terminated by the OS. Sometimes it happens when you press the back button and sometimes when you press the home button. What is the logic behind this?
My app must be keep activated during a long period (when you run it the app must stay resident). Also another question is if it is possible to popup when an event comes in and activate the screen and bring it to the front.
Does somebody made this already? Or is there more information about this (how to do it). Search the internet but doesn't find some useful things.
Pressing the Back button on the main activity will finish and exit the app.
Pressing the Home button will pause and leave the app in the background, it will return to the previous state when it's restored.
Override the onBackPressed() method on the main activity if you don't want it to quit when the back button is pressed.
If you want an event to occur when the screen is shown, override the onResume() method and do the checks and event required in there.
Being that your answer is with Flex the issue is that you need to listen to the stage's keyDown event.
stage.addEventListener(KeyboardEvent.Key_DOWN, yourHandler);
Then you need to response to the back button and inhibit this - if your intent is to stop the back button.
private function _onStage_keyDownHandler(event:KeyboardEvent)
{
if(event.keyCode == Keyboard.BACK)
{
event.stopImmediatePropogation();
event.preventDefault();
//your code here
}
}
And one more moment - android can close your app when it thinks that it need more memory for more recent apps.
And you may need to use native extensions or even develop your app in java if you want to implement reliable resident behavior.
Update: java service+ air ui example:
http://www.jamesward.com/2011/05/11/extending-air-for-android/
end of update
And you must know that air apps eat at least 30mb of memory(if they are empty and do nothing), normally they will start from 50-70mb. I guess that no user will wish to allow another 70mb of his memory-hungry device to be eaten by something not critical.
And for automatic maximizing of your window you may need root access.

re-lunching an android app from list of recent apps vs pressing on the app icon

I have written an Android app that plays some audio. There is a stop button on the app GUI that when I first lunch the app works fine.
However when I go out of the app while audio is playing and come back depending on how I came back the STOP button works or not.
If I come back to the app by holding the home button and seeing the list of recent apps and choosing my app from there, then the STOP button works. But if I click on the app luncher icon the STOP button does not work.
What is the difference between these two method and how can I make the re-lunch of the app by pressing on the app icon to behave similar to when I re-lunch the app by choosing the app from the list of recent lunched apps.
Without seeing the code we can't be entirely sure, but it sounds like what is happening is that your activity is you set up an action listener (setOnClickListener) on your stop button in your onCreate() method.
The onCreate() doesn't get called again if the app is never recycled (Android will do this when your app is put into the background) and started over.
When your app is put into the background onPause() will get called, then coming back from that you will get a call to onResume(). If your app has been in the background longer or Android needed more resources you'll get a call to onStop hitting the home button and onStart when the app opens again.
You'll need to do some investigation into your code as to why your listener goes away, but now you have the hooks to make sure they are connected back up when you're app is back.

Categories

Resources