0

I have a problem with back button. He doesn't work. When I go from first activity to second and from second to third back button don't want work. I used :

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        moveTaskToBack(true);
        Log.v("Co1s", "Cos1");
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

But when I push the button I get only:

05-23 10:45:57.863: W/KeyCharacterMap(238): Can't open keycharmap file 05-23 10:45:57.863: W/KeyCharacterMap(238): Error loading keycharmap file '/system/usr/keychars/pm8058-keypad.kcm.bin'. hw.keyboards.0.devname='pm8058-keypad' 05-23 10:45:57.863: W/KeyCharacterMap(238): Using default keymap: /system/usr/keychars/qwerty.kcm.bin

And this log in this function is not showing in logs. Why?

Edit: acrivity nr1:

protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.services_description);

        back_button = (Button) findViewById(R.id.service_back_button);
        back_button.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                intent = new Intent(services_description.this, service.class);
                TabActivityGroup parentActivity = (TabActivityGroup)getParent();
                parentActivity.startChildActivity("menu_activity", intent);     
                finish();
            }
        });
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            Log.v("Co1s", "Cos1");
        }
        return super.onKeyDown(keyCode, event);
    }

and second:

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.service);

    title = (TextView) findViewById(R.id.serviceTitle);
    description = (TextView) findViewById(R.id.serviceDescription);
    nextPage = (ImageView) findViewById(R.id.serviceNextPage);
    back_button = (Button) findViewById(R.id.service_back_button);


    nextPage.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            intent = new Intent(service.this, services_description.class);
            TabActivityGroup parentActivity = (TabActivityGroup)getParent();
            parentActivity.startChildActivity("menu_activity", intent);     

        }
    });
    back_button.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {

            finish();

        }
    });
}
@Override
public void onBackPressed() {
    Log.v("Cos", "Cos");
    finish();
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        moveTaskToBack(true);
        Log.v("Co1s", "Cos1");
        return true;
    }
    return super.onKeyDown(keyCode, event);
}
edi233
  • 3,511
  • 13
  • 56
  • 97
  • In which of your two is it the back button isn't working? If your first activity you both attach an `onClick` listener to the back button, and at the same time you override `onKeyDown`. And in your second activity you override both `onBackPressed` and `onKeyDown`. It all seems a bit fishy... Seems like you're doing lots of different things without knowing what you're _really_ doing, so my advice: remove all the "fluff" and start with an as simple as possible example. Get the back button working, and then start adding one event handler at the time to get the logging working as well. – Julian May 24 '12 at 07:41

3 Answers3

1

you can override the back button by method onBackPressed() also you can you the intent in this method to move any activity in the app. More you can get from onKeyDown() or onBackPressed() this link. This will provide you how we can do this

Community
  • 1
  • 1
Naresh Sharma
  • 4,323
  • 7
  • 48
  • 68
0

You shouldn't have to do anything to get the back button working. Just remove the whole block if (keyCode == KeyEvent.KEYCODE_BACK) etc, and you should be all good.

If you for some reason need to log when the back button is clicked, I think you could do it like this:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        Log.v("Co1s", "Cos1");
    }
    return super.onKeyDown(keyCode, event);
}

The important thing is that you pass on the "handling" of the click on the button to the overridden method and let the Android OS itself handle going back to the previous activity.

Julian
  • 20,008
  • 17
  • 77
  • 108
  • This is not working. I don't get any log. Only that warning, but only when I first push the button. – edi233 May 23 '12 at 09:10
  • @edi233: But is your back button working as expected, i.e. going back to the previous activity? I assume that's the main part here... And you say you don't get any log entry: have you tried setting a break point in your `onKeyDown` method? What happens when you run through in debug mode? What is the value of `keyCode` etc? You need to give us a bit more information... – Julian May 23 '12 at 09:12
  • No. It still don't work. I want to use this log to see if when I click on the button log show me. Debug mode doesn't start when I click. I don't know why. In other projects your way work perfectly but in this case not. – edi233 May 23 '12 at 09:19
  • @edi233: could you elaborate what you mean by "it still don't work"? The back button _should_ work "out of the box", without the need to add any key event handlers. What happens if you remove the `onKeyDown` method completely, do you move back to the previous activity then? You should post a bit more of your code, preferably a complete example that reproduces the problem. – Julian May 23 '12 at 09:22
  • it's still don't work means that when I click application don't back to previous activity. I add some code to see how I start new activity. Maybe there is the problem – edi233 May 23 '12 at 09:26
0

You can override onBackPressed() in your activity instead of overriding onKeyDown()

Vladimir
  • 9,683
  • 6
  • 36
  • 57