Handling the back button in a meteor android app - android

I have built a Meteor app with a mobile app interface (using Ratchet), only designed to be run as an app. On each page, there is a "back" link that takes you back to the parent page. For example, if I go deep inside a hierarchy of page such as this one:
Home > Category > Post
I can use a link in the Post page that will take me back to the Category page. Now, the problem is, if from this Category page I hit the back button, it should have the same behaviour as clicking the back link on the page. (in this case, take me to the Home page) Sadly, this doesn't happen and I get taken back to the Post page.
On iPhone it is not a problem (as far as I know) since there is no actual back button built-in to the device. But on Android it gives me headaches, for example:
Say a user goes to a Post page that he can delete using a button shown on the page. When the post-deletion method finishes, I take the user back to the Category page:
Router.go('categoryPage', {'_id': categoryId});
Problem is: if the user hits the back button after deleting a post, he or she gets taken to a "not found" page, since the previous post has been deleted. Now I can avoid that by adding replaceState: true like this:
Router.go('categoryPage', {'_id': categoryId}, {replaceState: true});
But now when the user hits the back button from the Category page, he or she gets taken to the page that was there before the Post page, which was... the same Category page. So the button just does nothing on the first press.
I also tried to pushState the url of the desired page in each of my template's `rendered̀€ function, to no avail (and what would I put in there for the Home page?):
Template.categoryPage.rendered = function () {
history.pushState(null, null, Router.url('home'));
};
Template.postPage.rendered = function () {
history.pushState(null, null, Router.url('categoryPage', {'_id': this.data.categoryId}));
};
Has anyone tackled this issue and/or would be able to drop some knowledge?

As soon as the back button is pressed, Cordova trigger a "backbutton" event, see there.
By listening to this event you should be able to override the default behavior and do what you want:
document.addEventListener("backbutton", onBackButtonDown, false);
function onBackButtonDown(event) {
event.preventDefault();
event.stopPropagation();
// Do what you want
...
}

Related

Xamarin forms : Stop page being accessible from hardware back button

I am using xamarin forms prism.
Quick info : I have a page that contains information, this page the user can delete along with its information, this page is an instance of a page called saved conversation, think of each instance as a saved email, you may have many of the one type and are made dynamically.
The user deletes one saved conversation and this also removes an instance for that page, after they have deleted it, it will send them back to one of two pages every time. But they are still able to access this deleted saved conversation page using the hardware back button (Android), either by clicking it straight after they were force navigated back or navigating the app a bit and then pressing the back button multiple times. I would use something such as...
protected override bool OnBackButtonPressed()
{
return true;
}
But I want the back button to work for other pages and only not work if the previous page is an instance of the Saved conversation page, regardless if it has been deleted or not (As I think I may be able to figure out if it was deleted or not myself, just need to detect the page the hardware back button is going to send the user to, I think).
This might fall under a user experience decision over a technical workaround; but it sounds to me like this page you have might be a good candidate to be a Modal Page.
Navigating away from the modal page (popping it from the stack) should accomplish the same goal you are aiming for: not being able to navigate "back" to that page.
What I have done was add this code to all the pages in which I did not want the user to be able to go back. But it would only limit them from going back if the previous page is one of the two shown below. If it isn't then the back button works.
What I did note was you have to make sure modalNavigation is set to false when navigating to the pages (in my case conversation and saved conversation) otherwise they won't appear on the navigation stack to check against.
/// <summary>
/// Will override the harware back button.
/// </summary>
/// <returns> True if it can't go back false if it can.</returns>
protected override bool OnBackButtonPressed()
{
int count = this.Navigation.NavigationStack.Count;
bool shouldntGoBack = false;
if (count > 1)
{
string previousPage = this.Navigation.NavigationStack[count - 2].ToString();
if (previousPage == "NGT.Views.ConversationPage" || previousPage == "NGT.Views.SavedConversationPage")
{
shouldntGoBack = true;
}
}
return shouldntGoBack;
}

How to keep the current page using Android WebView

I'm junior of android developer. (super beginner T_T)
I'm having trouble with Android Webview.
I am making a Hybrid app.
- when touch the app icon, it is linked to the webview(webview.loadurl(url)) directly.
When I click the home button after loading url and searching the web page (which is loaded through the webview),
and then re-click the app icon or touch the webview processing through click the home button long~.
I want to keep the page I've seen right before click the home button.
but It doesn't work.
Just reload the main index page.
I want...
Index page(login.jsp) -> A page -> B page -> Home button -> re-click the app or ~ -> B page
but unfourtunately, it works
Index page(login.jsp) -> A page -> B page -> Home button -> re-click the app or ~ -> Index page -> back key -> B page... It really strange.. \T_T
(back key -> B page :: Session didnt fired. may be keeped.)
I searched a lot about these problems.
Many people answered - Using Cookies and keep the session. but it also didn't work for me.
I know I didnt explain current problem well and show the code to you.
but I really really need help .
So please give me any hint.
Do I have to save the url adress which I searched last.?
Just to make sure we're talking about the same thing: when you say "home button," you mean the Android home button, not something on the web page, right?
When you press the home button, your activity is stopped; and it is restarted when the user returns to your app. The activity does not necessarily get destroyed and recreated, but it could, in which case the state is lost (except for state that the activity automatically saves).
I don't know about webview specifically, but if your activity is losing important state, such as the URL of the B page, you can save and restore it by overriding onSaveInstanceState() and onRestoreInstanceState().

Close the application on back button of android phones

I'm developing a app using angularjs and ionic framework. It has a logout option also. When i logout from my app it goes straight to the home page.But if i press the back button it'll go to page that can be accessed only if you are logged in. How can i create a option to close the app if you press the back button from the home page? I use angularjs.
I used this way but did not work
document.addEventListener("backbutton", function(e){
if($.mobile.activePage.is('/templates/playlists')){
/*
Event preventDefault/stopPropagation not required as adding backbutton
listener itself override the default behaviour. Refer below PhoneGap link.
*/
//e.preventDefault();
navigator.app.exitApp();
}
else {
navigator.app.backHistory()
}
}, false);
Your code is good.
But, I'm not sure about the second line : are you sure that the active page id is /templates/playlists ?
Normally, the value you should pass to the $.mobile.activePage.is function should contain a page id (which could be retrieved like this : $.mobile.activePage.attr('id')).
(by the way, I don't know which of jQuery Mobile you are using, but the $.mobile.activePage.is was deprecated).

Correct page navigation on Phonegap / jquery mobile app

I am working with phonegap and jquery mobile but am having problems with going back on pages. There are four pages:
Splash
Login
Main
SubMain
So Lets say the flow goes like this always hit the splash screen first. If a user has never logged before, they are shown the login screen, if they have then they are shown the main page. Here's a scenario:
A new user will navigate like this:
Splash
Login
Main
SubMain
Now from Submain screen, if they want to exit they will hit the back button until the they exit. However this route will take them through the Login page and the Splash screen again, when I want them to exit the app when they hit back on the main screen.
Is there anyway to make the user exit when pressing back on Main?
I realize I could avoid this by disabling the back button and adding an exit button.
I'm not sure if this matters or not, but all my pages are in one single html file separated by divs with the data-role="page" attribute
I think navigator.app.exitApp() is what you're looking for. As we keep track of the history manually our helper function looks similar to this:
function backKeyDown(){
if(historyContainsInfo(){
popHistory();
}else{
navigator.app.exitApp();
}
}
and is registered via document.addEventListener("backbutton", backKeyDown, true);

Reset history in html when on home page?

Okay, I'm making app with phonegap, and in the app I have a home button which goes to the home screen of the app, but if I click the back key after I touched the home button it goes to the page I was on before I clicked the home button, is it possible to reset the history when you navigate to the homepage? Thanks :)
You can't clear the history.
You can listen for back button event:
document.addEventListener("backbutton", onBackKeyDown, false);
function onBackKeyDown() {
// Handle the back button
}
and stop the propagation.
The link below recommends using location.replace(url) instead of replacing the history completely, which I'm not sure is possible anyway. However, given that this is a constrained environment (an app) wiping the history might make sense - again, if possible if you're in an app.
Clear browser history
Alternatively, couldn't you just remove the back key and replace it with a custom one?
inside the event handler of the button which takes you to home:
startActivity(activityForHome); //I guess you already have this line
finish(); //now add this line

Categories

Resources