BackAndroid 'hardwareBackPress' event is not working - android

According to react-native docs BackAndroid component with simple callback should not allow exit the app on back button press, but it looks like the event listener isn't called at all.
BackAndroid.addEventListener('hardwareBackPress', function() {
return true;
});
What should be changed to allow event listener triggering?

Based on the original github issue, Satyajit Sahoo provided a workable solution:
add the following to MainActivity.java:
#Override
public void onBackPressed() {
if (mReactInstanceManager != null) {
mReactInstanceManager.onBackPressed();
} else {
super.onBackPressed();
}
}

You are missing this.goBack(); before you return true.
BackAndroid.addEventListener('hardwareBackPress', function() {
this.goBack();
return true;
});

Related

How to notify a NativeScript Vue app when a Key Event is captured at the Activity level?

I have extended androidx.appcompat.app.AppCompatActivity to capture key event in a NativeScript-Vue project. Now, my problem becomes how to notify the Vue app that a key even has been captured. More importantly, what is the right way to notify the Vue app ('cause I can think of some not too correct way to solve the problem)?
At the end, I settled at the most straightforward solution. In activity.android.js, I implemented a simple listener pattern:
androidx.appcompat.app.AppCompatActivity.extend("org.myApp.MainActivity", {
keyUpListener: null,
registerListener(name, func) {
if (name == "keyUp") {
this.keyUpListener = func;
}
else {
throw new Error(`There's not a type of listener called ${name}.`);
}
},
unregisterListener(name) {
if (name == "keyUp") {
this.keyUpListener = null;
}
else {
throw new Error(`There's not a type of listener called ${name}.`);
}
},
onKeyUp: function (keyCode, event) {
if (this.keyUpListener) {
let result = this.keyUpListener(keyCode, event);
if (result != null) {
return result;
}
}
return superProto.onKeyUp.call(this, keyCode, event);
},
[... snip ...]
Then, in my .vue file, I simply register a listener to the extended Activity:
import { android as androidApp } from "tns-core-modules/application";
[... snip ...]
androidApp.foregroundActivity.registerListener(
"keyUp",
this.onKeyUp
);
If there's a better Vue or NativeScript way to handle this problem, I would love to learn about it.

Nativescript - Android disabling physical device back button

I am trying to disabling the physical device back in android only in some screens. Trying the below code is not working. Any idea?
import { RouterExtensions } from "nativescript-angular";
import * as application from "application";
import { AndroidApplication, AndroidActivityBackPressedEventData } from "application";
import { isAndroid } from "platform";
export class ItemComponent implements OnInit {
constructor(private router: Router) {}
ngOnInit() {
if (!isAndroid) {
return;
}
application.android.on(AndroidApplication.activityBackPressedEvent, (data: AndroidActivityBackPressedEventData) => {
data.cancel = true; // prevents default back button behavior
});
}
}
#Override
public void onBackPressed() {
if (!shouldAllowBack()) {
doSomething();
} else {
super.onBackPressed();
}
}
Add this code in your activity-
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// Your Code Here. Leave empty if you want nothing to happen on back press.
}
For more information visit this link.
Back button is controlled at Activity level, NativeScript uses one single Activity and all your pages / routes are fragments inside that.
So you don't have to call it on every page / component. Add the listener to your app component and check the router to know which page you are at and cancel the back event.
Use location and check if location consists the desired path(as mentioned in routing file) where you want to use your custom function.
import { Location } from '#angular/common';
constructor(private _location: Location
){}
if (application.android) {
application.android.on(AndroidApplication.activityBackPressedEvent, (data: AndroidActivityBackPressedEventData) => {
const path = this._location.path();
console.log(`path i s--> ${path}`);
switch (path) {
case '/Screen 1':
data.cancel = true;
break;
case '/Screen 2':
//do something else
break;
});
}

Remove back button if platform is Android

i have a problem in my app. (i'm using Ionic v1)
I want to hide the back button if the platform is Android, but if is iOS i want to show it.
I have the following code:
.config(function($ionicConfigProvider, $urlRouterProvider) {
// if none of the above states are matched, use this as the fallback
$urlRouterProvider.otherwise('/app/login');
if (ionic.Platform.isAndroid()) {
$ionicConfigProvider.tabs.style("tabs-icon-top tabs-striped");
$ionicConfigProvider.tabs.position("top");
$ionicConfigProvider.navBar.alignTitle("center");
}
else if (ionic.Platform.isIOS()) {
$ionicConfigProvider.backButton.text('').icon('ion-arrow-left-c');
$ionicConfigProvider.tabs.style("tabs-icon-top tabs-striped");
$ionicConfigProvider.tabs.position("top");
$ionicConfigProvider.navBar.alignTitle("center");
}
});
Thank you!
Put your code inside $ionicPlatform.ready method:
$ionicPlatform.ready(function(){
if (ionic.Platform.isAndroid()) {
$ionicConfigProvider.tabs.style("tabs-icon-top tabs-striped");
$ionicConfigProvider.tabs.position("top");
$ionicConfigProvider.navBar.alignTitle("center");
}
else if (ionic.Platform.isIOS()) {
$ionicConfigProvider.backButton.text('').icon('ion-arrow-left-c');
$ionicConfigProvider.tabs.style("tabs-icon-top tabs-striped");
$ionicConfigProvider.tabs.position("top");
$ionicConfigProvider.navBar.alignTitle("center");
}
});

How to identify back button event in Xamarin cross-platform (Xamarin.Forms)

Is there any way to know when the 'back' button event on the 'Android phone' is pressed? I'd like to exit the game and add few functionality to it when this button is pressed in Xamarin.Forms.
I googled a bit regarding this, but I got articles regarding Xamarin.Android Back button but not for Xamarin.Forms.
As I am relatively new to Xamarin.Forms, please help me out
public override void OnBackPressed()
{
//base.OnBackPressed();
}
Same thing I want in Xamarin.Forms. Need some assistance, guys.
If you mean Xamarin.Forms by "Xamarin Cross Platform", there is a OnBackButtonPressed event that you can use. As seen on that documentation:
Event that is raised when the hardware back button is pressed. This
event is not raised on iOS.
Simply override this event on your NavigationPage and you're ready to go:
protected override bool OnBackButtonPressed()
{
// Do your magic here
return true;
}
Good luck!
In my xamarin forms app you need to find the NavigationStack of the current Page if you are using master page:
public bool DoBack
{
get
{
MasterDetailPage mainPage = App.Current.MainPage as MasterDetailPage;
if (mainPage != null)
{
bool doBack = mainPage.Detail.Navigation.NavigationStack.Count > 1 || mainPage.IsPresented;
//top level of page and the Master menu isn't showing
if (!doBack)
{
// don't exit the app only show the Master menu page
mainPage.IsPresented = true;
return false;
}
else
{
return true;
}
}
return true;
}
}

Titanium "addEventListener" is called multiple times but I only need it once

I am using this code in Titanium SDK 3.0.0.GA when I click on any button (left_btn or right_btn) once the eventListener(scrollView.addEventListener('scroll',function(){});) is called multiple times.
How can I fix this issue?
right_btn.addEventListener('singletap', function() {
scrollView.scrollToView(scrollView.currentPage + 1);
});
left_btn.addEventListener('singletap', function() {
scrollView.scrollToView(scrollView.currentPage - 1);
});
scrollView.addEventListener('scroll', function() {
alert("scroll view");
});
Please, help me!
Thanks in Advance.
You can use removeEventListener to remove event after doing what you want in event function.
function scrollEvent() {
alert('scroll view');
scrollView.removeEventListener('scroll', this);
}
scrollView.addEventListener('scroll', scrollEvent);
Or declare a temporary boolean variable to determine if event is already fired.
var fired = false;
scrollView.addEventListener('scroll', function() {
if (!fired) {
alert('scroll view');
fired = true;
}
});

Categories

Resources