ionic v4 back button canGoBack alternative - android

I have an ionic application that is currently based on version 4. In version 4 they switched to angular routing model and basically parts of the NavController were deprecated and I'm looking for the proper replacement for NavController.canGoBack.
To give some context I basically have an app that requires a pin code unlock prior to showing application contents, on my pin-code page I already implemented a guard to prevent deactivation (basically prevent). However, while implementing the back button I noticed that when I press the back button on the very first page. It seems to bypass the guard, and instead the screen turns blanc and then shows the pin code page again.
I have my suspicions that when I issue an this.navCtrl.back(); on it refreshes the page and it doesn't actually use the javascript based routing. So to that end I was hoping I would be able NavController.canGoBack to give me some indication if there's anything to navigate back to.

To detect if there's anything to navigate back to you will have to implement a service that saves the history of previous routes in an array.
One implementation of such a service can be found here.

The simplest would be importing
import {Location} from '#angular/common';
and then
constructor( private _location: Location,) { }
this._location.back() // this would route back if back is possible

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

How to determine if Webview in React Native Single Page Application can go back on Android?

Suppose we have an Android app, then we can call canGoBack() to determine if the back button should go back or exit the app.
Suppose we have a RN Android app that is a multi page application, then onNavigationStateChange() will return if canGoBack is true or false.
But if it is an RN single page application, onNavigationStateChange is not fired on clicking a link.
There is a workaround shared here, which involves overriding history.pushState.
I was also thinking of querying the source/url/html properties of the webview but somehow they all return null. I'm not sure if this is expected or not.
Are there any better solutions to understanding if the back button should exit the app or bring the user to the previous screen?

Open ACTION_USAGE_ACCESS_SETTINGS screen for specific Android App

I want ACTION_USAGE_ACCESS_SETTINGS for my app locker Android application without 2 steps navigation. I mean can I get it in background like media permissions or direct access to my app ACTION_USAGE_ACCESS_SETTINGS screen?
After digging around in the source code of the Android settings app I think that this is not possible. The screenshot you posted is the UsageAccessDetails Fragment and the only place where it gets created is on line 610 in the ManageApplication class right here, which in turn is in a method that only gets invoked by the onItemClick method of the ManageApplication class right here. So I think that the UsageAccessDetails Fragment only gets created when a user actually clicks on the app in the UsageAccessSettingsActivity of the Settings app.

Android: how to know previous app

I have 2 different applications A and B and I want to create a special animation from B to A, that is when A is opened after B was visible. This means that I need to somehow know the previous app after which my app was opened. I can have different scenarios of going from B to A - using Recent Apps (multitasking) button, using Back button, using Home button (application A is a custom home screen). Are there any ideas how to do this? Some functions in ActivityManager might help, but they have comments in documentation saying not to use them for implementing logic and control flow.
Not sure if this will work across different applications, but how about getCallingActivity() or getCallingPackage()?
If that doesn't work, could you pass along some 'extra' data in the bundle when you launch the intent that indicates the launching application?
I managed to figure out how to implement this, its working for me.
I used this answer, but replaced the ActivityManager.getRunningTasks() with ActivityManager.getRecentTasks() supplying RECENT_WITH_EXCLUDED | RECENT_IGNORE_UNAVAILABLE, and took the component name from baseIntent member of the result. The info at index 1 is the one that was running before the current app was opened, irrelevant of the fact how you get back to your app - back button, home button, recents button or opened from another app.
NOTE: This works when your app is started, like in onResume() but doesn't work when your app is closed (when called in onPause()) because the new task is not yet loaded into the activity manager. So if you need to also know to which app are you going then it might be a bit more complex.
NOTE2: Although the documentation tells not to use the API above for any kind of logic and control flow, I saw that the multitasking/recent app's code is doing exactly the same, so in my opinion it should not be as risky as they write it docs.
NOTE3: Don't forget to follow all the steps in the answer I mentioned above, like adding needed permissions, otherwise you will get exceptions. Being part of the system in my case makes it much easier for me.

Go back to the Android call screen

As it seems that you cannot end a call directly from your application, I want to know if there's a way to go back to the android call screen from my application. So the user just have to press a button on my application and be redirected to the android call screen to hang up the call ?
I think you can... it's just not easy.
If you have a ITelephony.aidl you can interact with it.
Check this:
Reflection to access advanced telephony features
Some other examples in this project:
http://code.google.com/p/teddsdroidtools/

Categories

Resources