tags:

views:

63

answers:

3

Hi guys.

I am using ViewFlipper for my application. I trying to make it : when user pressed on escape (back) button, it would be back to parent Layout.

In other words, just like this :

             Activity
                |
 Layout1 -> Layout2 -> Layout3 
 if user pressed on escape, go back to parent:
 Layout1 <- Layout2 <- Layout3

How to make it ?

Please advice.

Thanks.

A: 

Have you tried ViewFlipper.showNext() and ViewFlipper.showPrevious()?

fupsduck
Yes, But when I press on the back, Activity will be stopped.
AndroiDBeginner
A: 

You will need to handle KeyEvent with KEYCODE_BACK something like ( in your activity):

   public boolean dispatchKeyEvent(KeyEvent event) {
    if (event.getAction() == KeyEvent.ACTION_DOWN) {
        switch (event.getKeyCode()) {
            case KeyEvent.KEYCODE_BACK: 
                 if (/*has more views to show*/){  
                         viewFlipper.showPrevious();  
                         return true  
                 }else{
                    return super.dispatchKeyEventt(event)
                 }
                 //rest of the code here
Nikolay Ivanov
This technique should not be used as of Android 2.0. Use onBackPressed() starting with that version of Android.
CommonsWare
+1  A: 

You might find this useful: Back and the other Hard Keys - three stories

Samuh
Thanks. It helped me.
AndroiDBeginner