This is a migrated thread and some comments may be shown as answers.

Is there an equivalent moveTaskToBack function

5 Answers 86 Views
General Discussion
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Joey Navarro
Top achievements
Rank 1
Joey Navarro asked on 12 Nov 2013, 10:36 PM
I'm trying to prevent the back button on an android app from exiting the app.

5 Answers, 1 is accepted

Sort by
0
Markus
Top achievements
Rank 2
answered on 13 Nov 2013, 07:21 AM
Dear Joey

I use the back butten exactely to close the app. 

http://cordova.apache.org/docs/en/3.0.0/cordova_events_events.md.html#backbutton

So I assume you could simply do: 

<script type="text/javascript" charset="utf-8">
 
    // Wait for device API libraries to load
    //
    function onLoad() {
        document.addEventListener("deviceready", onDeviceReady, false);
    }
 
    // device APIs are available
    //
    function onDeviceReady() {
        // Register the event listener
        document.addEventListener("backbutton", onBackKeyDown, false);
    }
 
    // Handle the back button
    //
    function onBackKeyDown() {
//simply to do nothing :-)
    }
 
    </script>


Markus

0
Joey Navarro
Top achievements
Rank 1
answered on 13 Nov 2013, 04:50 PM
No, I don't want the back button to close the app. I want it to behave the same way as if the home button was pressed.
0
Markus
Top achievements
Rank 2
answered on 14 Nov 2013, 06:43 AM
Dear Joey

Sorry for beeing a bit unclear on my answer. I just wanted to give you the code I am using to capture the back button usage by a user. 

In my case I do show an confirm to close. But you could do what ever you want here: 

function onBackKeyDown() {
//simply to do nothing :-)
    }
  
    </script>


That's why I wrote //simply do nothing.

The above code should capture the case where a user presses the backbutton and will call the onBackKeyDown() function. And I would assume if you do nothing there the backbutton will do exactely this - nothing and therefore not close the app.

Markus
0
Joey Navarro
Top achievements
Rank 1
answered on 14 Nov 2013, 05:30 PM
You're right. The code you wrote will prevent the app from exiting. But what I really wanted was for the app to go to the background as if the home button was pressed. So I needed a method to call inside the onBackKeyDown function to accomplish this. The native Android API has a moveTaskToBack function that doesn't seem to be implemented in Phonegap.
0
Markus
Top achievements
Rank 2
answered on 15 Nov 2013, 06:30 AM
Sorry Joey

Since I did not know the moveTaskToBack function I just read your first line: I'm trying to prevent the back button on an android app from exiting the app.

Just out of couriosity. Why do you want the back button to do the same button just next to it does by default. 

- Would a user know why the back would do the same as home
- Wouldn't a user simply use the home button to go home

The most apps I have on my phone do either go back or ask if you want to close.

Markus
Tags
General Discussion
Asked by
Joey Navarro
Top achievements
Rank 1
Answers by
Markus
Top achievements
Rank 2
Joey Navarro
Top achievements
Rank 1
Share this question
or