Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
285 views
in Technique[技术] by (71.8m points)

ionic4 - Ionic 4 alternative for platform.registerBackButtonAction

I looked around the new platform for the Ionic 4, it seems like the registerBackButtonAction function was removed from it.

Are there any other alternatives to handle the Android hardware back button?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Update: This was fixed in v4.0.0-beta.8 (dfac9dc)


Related: how to integrate hardware back button into ionic4 navigation


This is tracked on GitHub, in the Ionic Forums and Twitter
Until there is an official fix, you can use this workaround:

this.platform.backButton.subscribe(() => {
  // code that is executed when the user pressed the back button
})

// To prevent interference with ionic's own backbutton handling
// you can subscribe with a low priority instead
this.platform.backButton.subscribeWithPriority(0, () => {
  // code that is executed when the user pressed the back button
  // and ionic doesn't already know what to do (close modals etc...)
})

Be aware that you need to save the result of subscribe(...) if you ever want to unsubscribe from it again.


Old answer: (out of date as of April 2018)

registerBackButtonAction is just a wrapper for the corresponding Cordova call.

So you can just take your old call to registerBackButtonAction:

this.platform.registerBackButtonAction(() => { 
  // code that is executed when the user pressed the back button
});

and replace it with:

this.platform.ready().then(() => {
  document.addEventListener("backbutton", () => { 
    // code that is executed when the user pressed the back button
  });
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...