开发者

can we restrict backbutton functionality to given levels?

i am fighting with android hardware backbutton开发者_如何转开发 now my question to you is can i restrict its functionality upto 3 or 4 levels?

right now for example, i have 1,2,3,4,5,6,7,8,9,10 activities

i go to 1 then 2 then 3 .... upto 10 and start pressing backbutton

it takes me to 9 then 8 then 7 upto end

i want to keep track of only last three levels is this possible or not?

any help would be appreciated.


i want to keep track of only last three levels is this possible or not?

You are welcome to keep count, perhaps using an integer. Use onBackPressed() (Android 2.x) and onKeyDown() (Android 1.x) to detect BACK button presses. If you chain to the superclass, normal BACK processing will occur. If you do not chain to the superclass, normal BACK processing will not occur.

Now, bear in mind that the user can press other keys, like HOME, or the user might respond to a notification (e.g., incoming text message). In those cases, it is possible that your counts may get out of sync with the actual user navigation. Hence, I encourage you to find some other UI pattern that does not involve artificially restricting the BACK button between activities in the fashion that you have outlined here.


This is the example code to CommonsWare's answer "onKeyDown() (Android 1.x)"

public boolean onKeyDown( int keyCode, KeyEvent event ) {


    if (keyCode == KeyEvent.KEYCODE_BACK ) {
      //do some stuff
      return false;
    }

    return super.onKeyDown(keyCode, event);
  }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜