活动和后退按钮

发布于 2024-09-30 10:15:17 字数 228 浏览 6 评论 0 原文

在用户界面上我显示了一个后退按钮。有 A、B、C 三个活动。活动 A 是闪屏。 Activity B 是主屏幕。活动 C 是编辑屏幕。仅在编辑屏幕上我有一个后退按钮。当用户按下时,我打开活动 B,其中还包含某种 http 请求和响应。我希望显示堆栈中的前一个活动?我怎样才能做到这一点?我可以通过按设备的后退按钮打开之前的活动吗?

当我按下设备上的后退按钮时,没有任何 http 请求?我想在按下 ui 后退按钮时实现此行为。

on the ui i am having a back button displayed. there are three activities say A,B,C. Activity A is splash screen. Activity B is home screen. Activity C is edit screen. on edit screen only i am having a back button. when the user press that i open the activity B which also contains some sort of http request and response. i want that the previous activity from stack should get displayed? how can i achieve that? i am able to open previous activity on pressing the back button of device?

when i press back button on device there doesnt goes any http request? i want to achieve this behaviour when i press the ui back button.

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

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

发布评论

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

评论(3

三生一梦 2024-10-07 10:15:17

我认为在您的后退按钮中,您可以调用活动 B 的意图,并且您的 http 请求和响应代码位于 onCreate 函数中,

但是设备上的后退按钮不会调用 onCreate
有两种解决方案,

如Macarse所说,听onKeyDown

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)  {
    if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
        Intent i = new Intent(ActivityC.this,ActivityB.class);    
            startActivity(i);   
        return true;
    }

    return super.onKeyDown(keyCode, event);
}

,第二种方法是在ActivityB的onStart上编写代码,

protected void onStart() {
        //http request and response code
    }

当ActivityB打开时,onStart将一直调用此

希望这对您有帮助

I think in your back button you may call the intent for activity B and your http request and response code is in onCreate function

But the back button on device will not call onCreate
There is Two solution for this

One as Macarse say, listen onKeyDown

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)  {
    if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
        Intent i = new Intent(ActivityC.this,ActivityB.class);    
            startActivity(i);   
        return true;
    }

    return super.onKeyDown(keyCode, event);
}

And the second method is to write you code on onStart of ActivityB

protected void onStart() {
        //http request and response code
    }

This onStart will call all the time this when ActivityB open

Hope this help you

〆一缕阳光ご 2024-10-07 10:15:17

首先,在 ui 中显示后退按钮并不好。 每个 Android 设备都有一个后退按钮。

如果您想以不同的方式处理后退按钮,请检查 此链接

First of all, it's not good to have a back button displayed in the ui. Every android device has a back button in it.

If you want to handle the back button in a different way, check this link.

无妨# 2024-10-07 10:15:17

使用 onKeyDown 的解决方案可能有效,但在我看来使用 onBackPressed 更容易。

您可以使用以下覆盖来拦截后退键:

@Override
public void onBackPressed()
{
   //logic here, for example an intent
   Intent intent = new Intent(this, ActivityB.class);    
   startActivity(intent);   
}

反之亦然,super.onBackPressed(); 返回到历史记录中的上一个 Activity。

The solution with onKeyDown might work but using onBackPressed is much easier in my opinion.

You can intercept the Back-key with following override:

@Override
public void onBackPressed()
{
   //logic here, for example an intent
   Intent intent = new Intent(this, ActivityB.class);    
   startActivity(intent);   
}

And for the other way around super.onBackPressed(); returns to previous Activity in history.

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