this.platform.registerBackButtonAction doesn't work

i try different app (conference app- blank app) its worked but i try my app it doesn’t work .
I’ve been struggling 2 days I could not find the cause

my app.component

rootPage: any;
public counter = 0;
constructor(
public platform: Platform,
public statusBar: StatusBar,
public splashScreen: SplashScreen,
public app: App,
public toastCtrl: ToastController,
public splitPane: SplitPane,
public menu: MenuController,

) {
this.rootPage=Welcome;
this.platformbegin();

}

platformbegin(){

this.platform.ready().then(() => {
  this.statusBar.styleDefault();
  this.splashScreen.hide();


  this.platform.registerBackButtonAction(() => {
    if (this.counter == 0) {
      this.counter++;
      this.presentToast();
      setTimeout(() => { this.counter = 0 }, 2000)
    } else {
      // console.log("exitapp");
     this.platform.exitApp();
    }
  }, 0)
});

}

presentToast() {
let toast = this.toastCtrl.create({
message: “Press again to exit”,
duration: 2000,
position: “bottom”
});
toast.present();
}

backToWelcome() {
const root = this.app.getRootNav();
root.popToRoot();
}

logout() {
//Api Token Logout

localStorage.clear();
this.menu.enable(false);
setTimeout(() => this.backToWelcome(), 1000);
this.nav.setRoot(Welcome);

}
openPage(page: PageInterface) {

this.nav.push(page.component, { item: page.index });

}
}

What do you want to achieve ?

As I can read your question it seems you want to detect if your Back Button is pressed and perform an action afterwards, right ?

If that isnt the case then please explain your problem more then the community may be able to help you out. :slight_smile::penguin:

Oh sorry i didnt solved problem all night.

this.platform.registerBackButtonAction(() => {
if (this.counter == 0) {
this.counter++;
this.presentToast();
setTimeout(() => { this.counter = 0 }, 2000)
} else {
// console.log(“exitapp”);
this.platform.exitApp();
}
}, 0)

This code doesnt worked on 6.3 android device. where can the problem be

Please help me,I’m about to go crazy. All plugins one by one uninstall and try and again install. So many option try but didnt work :confused:

I don’t know what you want to achieve. You have to explain what you want to create else I am not able to help you :frowning:

i want to double tab backbutton for exiting from application. but didnt worked. and not worked nav.push page stacking , and other back button features before. in any event back button is close the app

this.platform.registerBackButtonAction() function does not work when I use the back button

Can u help me :pray:

in any event back button is close the app

Its obvios why the application is closing all the time.

{
this.rootPage=Welcome;
this.platformbegin(); //<- !
}

You are initializing the function when the app is starting.
Resulting in = App closes because of: this.platform.exitApp();

[The problem could be that your counter is NOT working and it just runs the else code! Maybe adding a console.log would show if the counter is really working or not.]

  this.platform.registerBackButtonAction(() => {
  if (this.counter == 0) {
    this.counter++;
    this.presentToast();
    setTimeout(() => { this.counter = 0 }, 2000)
  } else {
    // console.log("exitapp");
    this.platform.exitApp();
  }
}, 0)

Bind this code somewhere in your typescript but not in the initialized function.
(Idk if your counter is working or not Im asuming it works for now, but I see a mistake in here.)

Simply add an else if.

  this.platform.registerBackButtonAction(() => {
  if (this.counter == 0) {
    this.counter++;
    this.presentToast();
    setTimeout(() => { this.counter = 0 }, 2000)

  } else if(this.counter == 2){
    // console.log("exitapp");
    this.platform.exitApp();
  }
}, 0)

Im also not sure why this: 0) is present in the end of your codeline.
You also should be able to initialize it again with your function with this code.


If that doesn’t work. You might just add a Backbutton in the HTML yourself and create a function for it and bind this code to it just without this.platform.registerBackButtonAction(() => {

I hope this helps you out a bit. :blush::bird: