在用户界面上我显示了一个后退按钮。有 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.
发布评论
评论(3)
我认为在您的后退按钮中,您可以调用活动 B 的意图,并且您的 http 请求和响应代码位于 onCreate 函数中,
但是设备上的后退按钮不会调用 onCreate
有两种解决方案,
如Macarse所说,听onKeyDown
,第二种方法是在ActivityB的onStart上编写代码,
当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
And the second method is to write you code on onStart of ActivityB
This onStart will call all the time this when ActivityB open
Hope this help you
首先,在 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.
使用 onKeyDown 的解决方案可能有效,但在我看来使用 onBackPressed 更容易。
您可以使用以下覆盖来拦截后退键:
反之亦然,
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:
And for the other way around
super.onBackPressed();
returns to previous Activity in history.