android 中是否有默认的后退键(在设备上)侦听器?

发布于 2024-08-28 03:46:35 字数 355 浏览 6 评论 0 原文

我有两个活动 A 和 B。当我单击 A 中的按钮时,将显示 B。当我单击 B 中的按钮时,它将返回到 A。我在 finish() 方法之后设置了 overridePendingTransition 方法。它工作正常。但如果当前的 Activity 是 B。当时我单击设备中的默认后退按钮。它显示从右到左的过渡以显示活动 A。

我如何收听设备上的默认后退键?

编辑:

Log.v(TAG, "back pressed");
finish();
overridePendingTransition(R.anim.slide_top_to_bottom, R.anim.hold);

I am having two activities A and B. when i click the button in A that will shows B. when i click the Button in B it backs to A. i had set the overridePendingTransition method after the finish() method. it works properly. but in case the current Activity is B. on that time i click the default back button in the device. it shows the right to left transition to show the Activity A.

How i can listen that Default back key on device?

EDIT:

Log.v(TAG, "back pressed");
finish();
overridePendingTransition(R.anim.slide_top_to_bottom, R.anim.hold);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

木格 2024-09-04 03:46:35
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)  {
    if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
        // do something on back.
        return true;
    }

    return super.onKeyDown(keyCode, event);
}

以下链接是 Android 开发人员自己编写的有关如何处理后退键事件的详细说明:

使用后退键

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)  {
    if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
        // do something on back.
        return true;
    }

    return super.onKeyDown(keyCode, event);
}

The following link is a detailed explanation on how to handle back key events, written by the Android developers themselves:

Using the back key

旧故 2024-09-04 03:46:35

对于Android 2.0及更高版本,Activity类中有一个具体的方法:

@Override  
public void onBackPressed() {
    super.onBackPressed();   
    // Do extra stuff here
}

For Android 2.0 and later, there is a specific method in the Activity class:

@Override  
public void onBackPressed() {
    super.onBackPressed();   
    // Do extra stuff here
}
转身以后 2024-09-04 03:46:35
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if(keyCode == KeyEvent.KEYCODE_BACK){
        //Do stuff
    }

    return super.onKeyDown(keyCode, event);
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if(keyCode == KeyEvent.KEYCODE_BACK){
        //Do stuff
    }

    return super.onKeyDown(keyCode, event);
}
转身泪倾城 2024-09-04 03:46:35

我在带有媒体播放器的活动中使用此代码。当用户按下后退按钮时,我需要停止播放,但仍然能够返回到之前的活动。

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)  {
    if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
        // do something on back.
        try{
            mp.stop(); //this line stops the player
            return super.onKeyDown(keyCode, event);//this line does the rest 
        }
        catch(IllegalStateException e){
            e.printStackTrace();
        }
        return true;
    }

    return super.onKeyDown(keyCode, event); //handles other keys
}

I use this code on an activity with a media player. I needed to stop the playback when user pressed the back button but still be able to go back to previous activity.

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)  {
    if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
        // do something on back.
        try{
            mp.stop(); //this line stops the player
            return super.onKeyDown(keyCode, event);//this line does the rest 
        }
        catch(IllegalStateException e){
            e.printStackTrace();
        }
        return true;
    }

    return super.onKeyDown(keyCode, event); //handles other keys
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文