2

I've got a function that makes use of the back button on Android Phones but for some reason when I use it, the app goes back 2 pages instead of the intended one.

This is what my code looks like at the moment:

function onBackKeyDown()
{
    currentPage = $.mobile.activePage.attr('id');
    if(currentPage == "createUser")
    {
        $.mobile.changePage("#logIn");
        alert("Return To Login Page");
        currentPage = "";
    }
    else if(currentPage == "cardDetails")
    {
        $.mobile.changePage("#createUser");
        alert("Return To Create User");
        currentPage = "";
    }
    else if(currentPage == "mainMenu")
    {
        $.mobile.changePage("#logIn");
        alert("Return To Log In");
        currentPage = "";
    }
    else if(currentPage == "locationPage")
    {
        $.mobile.changePage("#mainMenu");
        alert("Return To Main Menu");
        currentPage = "";
    }
    else if(currentPage == "editUserPage")
    {
        $.mobile.changePage("#mainMenu");
        alert("Return To Main Menu");
        currentPage = "";
    }
    else if(currentPage == "editCardDetailsPage")
    {
        $.mobile.changePage("#editUserPage");
        alert("Return To Edit User");
        currentPage = "";
    }
    else
        alert("TEST");
}

For example if I were to be incardDetailsand I pushed the back button it would go back tocreateUserand continue back tologIn. Is there anything in my code that would cause this, I've tried resetting the currentPage to empty and added in else thinking that it was going though each of them individually when they were just if statements

EDIT: Requested Java Code:

import android.os.Bundle;
import org.apache.cordova.DroidGap;

public class FnBApp extends DroidGap {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.main);
        super.loadUrl("file:///android_asset/www/index.html");
        }
    }

Any help would be appreciated

John

John
  • 115
  • 10
  • possible duplicate of [Android - How To Override the "Back" button so it doesn't Finish() my Activity?](http://stackoverflow.com/questions/3141996/android-how-to-override-the-back-button-so-it-doesnt-finish-my-activity) – StephenG Jul 23 '15 at 16:34

2 Answers2

0

You need to overload the android back button instead of just detecting and acting on it.

remove your key listener or return true when you have KEY_BACK

you just need the following to catch back key (make sure not to call super)

Also, if you plan on having a service run in the background, make sure to look at startForeground() and make sure to have an ongoing notification or android will kill your service if it needs to free memory

@Override
public void onBackPressed() {
   Log.d("CDA", "onBackPressed Called");
   Intent setIntent = new Intent(Intent.ACTION_MAIN);
   setIntent.addCategory(Intent.CATEGORY_HOME);
   setIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
   startActivity(setIntent);
}

Update: Changes for app specific psuedocode

public class FnBApp extends DroidGap {
    ....
    @Override
    public void onBackPressed() {
       Log.d("CDA", "onBackPressed Called");
       //if super.getUrl shows a specific page:
       if() {
           super.loadUrl("file://android_asset/www/index.html#login");
       };
    }

Android - How To Override the "Back" button so it doesn't Finish() my Activity?

Community
  • 1
  • 1
StephenG
  • 2,851
  • 1
  • 16
  • 36
  • Can I still use Java if I am using super.loadUrl for just straight Javascript, CSS and HTML in the application? – John Jul 23 '15 at 16:37
  • You'll need to overload that function in the java part of the app. It's impossible to do in straight Javascript – StephenG Jul 23 '15 at 16:39
  • If I overload it will it still run? My entire app is based on very minimal Java and very heavy on Javascript. Sorry about all the questions my knowledge in Java isn't as strong as Javascript – John Jul 23 '15 at 16:42
  • You'll have to overload it and then maybe use some IPC to send commands between Java and Javascript. If you can post more of your app, I might be able to help some more – StephenG Jul 23 '15 at 16:45
  • Ok the Java has been added – John Jul 23 '15 at 16:50
  • What purpose does the if function serve? Is it something like if the page is file://blahblah#createUser then I should substitute #logIn for #createUser in the load? – John Jul 23 '15 at 17:57
  • The function is meant to change the page from Java instead of javascript. By overloading the back button, you can change the page in Java using loadUrl instead of Javascript – StephenG Jul 23 '15 at 18:58
  • Changing the page is only half the issue, I need to be able to also clear out information stored in html tag containers as well. – John Jul 23 '15 at 19:26
  • So how exactly do I get the Url in your comments you said super.getUrl but I don't think that's a function – John Jul 23 '15 at 21:06
0

If you are using Cordova, maybe you want to use your code in other devices apart from Android? In which case you don't want to use any Java at all. Going back to the original question, would this help?:

    function onDeviceReady() {
    // Register the event listener
    document.addEventListener("backbutton", onBackKeyDown, false);
}

(taken from this post)

Community
  • 1
  • 1
quilkin
  • 874
  • 11
  • 31
  • I suppose I should have noted that I already have an deviceready and a backbutton Listener implemented. But I have solved my issue by just adding in custom back buttons and disabling the phones back button for the most part. The only function the phones back button provides now is to quit the application when the user in on the login screen – John Jul 26 '15 at 03:18