10

In Sencha touch if I use navigation view i can get back button. This is pretty fine.

But what if user hit device backbutton? it is direct exiting the applicaiton. In my requirement it should not exit the application it has to go back to previous screen.How can i do this?.

atluriajith
  • 762
  • 3
  • 17
  • 41
  • this is same problem I'm also facing. As all app is driven by single ( not necessary though ) html file there's no history of previously visited pages and app get closed on back key. There might be a way from phonegap if you are using. – SachinGutte Aug 03 '12 at 15:39
  • its an applicaiton not an single file to paste here. contains all files system and lot of files – atluriajith Aug 06 '12 at 05:43
  • @atluriajith Have you fixed the issue. If yes, can you tell me how you solved that. I am also having the same problem. I have gone through Sencha History/routing document but didn't get it properly. Although in jquery i have already implemented this and it was pretty simple. – Tarak Aug 17 '12 at 13:00
  • @Tarak not yet tarak.. for now i have added native confirmation alert.. I'm finishing the app if yes if not doing nothng.. refer this url http://docs.sencha.com/touch/2-0/#!/guide/history_support – atluriajith Aug 21 '12 at 10:56

2 Answers2

8

You can handle hardware back button like this:

if (Ext.os.is('Android')) {
    document.addEventListener("backbutton", Ext.bind(onBackKeyDown, this), false);

    function onBackKeyDown(eve) {

        eve.preventDefault();

        //do something
        alert('back button pressed');

    }
}
tonymayoral
  • 4,797
  • 2
  • 26
  • 27
6

I didn't find the instructions on the history support page that useful when trying to do this; I couldn't see anyway to use routes when dealing with a navigation view which can have a large stack of views on it at anytime.

If you just want the back button to work though, you can use the popstate and pushstate functions (see https://developer.mozilla.org/en-US/docs/DOM/Manipulating_the_browser_history for a reference). The idea is that you push a state when adding a view and pop it off when removing one. The physical back button on an Android phone, or the back button on a desktop browser effectively calls history.back(); so all you need to do is ensure that pressing the back button on the titlebar does the same, and it is that which triggers the nav view to pop.

To make use it work in Sencha Touch, I add the following to the main controller:

In refs I have references to the main view (an instance of Ext.navigation.View) and to its titlebar, from which you can hook onto the event of the back button e.g.:

refs: {
        main: 'mainview',
        mainTitleBar: 'mainview titlebar',
}...

I attach the following functions via the control config object..

 control: {
        main: {
            push: 'onMainPush'
        },
        mainTitleBar: {
            back: 'onBack'
        },
        ...

These are defined as:

  onMainPush: function(view, item) {
      //do what ever logic you need then..
      history.pushState();
  },
  onBack: function() {
      history.back(); //will cause the onpopstate event to fire on window..
      //prevent back button popping main view directly..
      return false;
  },

I then attach a function to execute when the state is popped via the init function of..

init: function() {
    /* pop a view when the back button is pressed
       note: if it's already at the root it's a noop */
    var that = this;
    window.addEventListener('popstate', function() {
        that.getMain().pop();
    }, false);
},

Now, pressing back on the titlebar, executes history.back(), this in turn fires the popstate event which then causes the main view to pop.

If you want to see this working on a real application, there is a (v. basic!) property finder app using this technique on github here.

Mark Rhodes
  • 10,049
  • 4
  • 48
  • 51