Android:捕获活动的返回
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/449484/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android: Capturing the return of an activity
提问by Chrispix
I have a question regarding launching new activities. It boils down to this. I have 3 tabs on a view
我有一个关于启动新活动的问题。归结为这一点。我在视图上有 3 个标签
A) contains gMap activity
B) camera activity
C) some random text fields.
Requirement is that the application runs in Portrait mode.
要求是应用程序以纵向模式运行。
All 3 tabs work as expected w/ the exception of the Camera Preview Surface (B). It is rotated 90 degrees. They only way to make it correct is to set the app to landscape which throws all my tabs around, and is pretty much unworkable.
所有 3 个选项卡都按预期工作,但相机预览表面 (B) 除外。它旋转了 90 度。他们唯一使它正确的方法是将应用程序设置为横向,这会抛出我所有的标签,并且几乎无法使用。
My solution is this : replace
我的解决办法是:更换
my camera activity with a regular activity that is empty w/ the exception of
我的相机活动与常规活动是空的,除了
Intent i = new Intent(this,CameraActivity.class);
startActivity(i);
This launches my CameraActivity. And that works fine. I had to do a linear layout and include 3 images that look like real tabs, so I can try and mimic the operation of the tabs while rotating the screen to landscape and keep the visuals as portrait. The user can click one of the images(buttons) to display the next tab. This is my issue. It should exit my 'camera activity' returning to the 'blank activity' in a tab, where it should be interpreted to click the desiered tab from my image.
这将启动我的 CameraActivity。这很好用。我必须做一个线性布局并包含 3 个看起来像真实标签的图像,所以我可以尝试模仿标签的操作,同时将屏幕旋转到横向并保持视觉效果为纵向。用户可以单击其中一个图像(按钮)以显示下一个选项卡。这是我的问题。它应该退出我的“相机活动”,返回到选项卡中的“空白活动”,在那里它应该被解释为从我的图像中单击所需的选项卡。
The main thing is, when it returns, it returns to a blank (black) page under a tab (because it is 'empty'). How can I capture the return event back to the page that called the activity, and then see what action they performed?
主要的是,当它返回时,它返回到选项卡下的空白(黑色)页面(因为它是“空的”)。如何将返回事件捕获回调用该活动的页面,然后查看它们执行的操作?
I can set an onclicklistener where I can respond to the fake tabs (images) being clicked to exit out of the camera activity. On exit, the tab should update so that is where you return. any Suggestions?
我可以设置一个 onclicklistener,在那里我可以响应被点击的虚假标签(图像)以退出相机活动。退出时,选项卡应更新,以便您返回。有什么建议?
Thanks,
谢谢,
回答by Reto Meier
I'll focus on answering how to resolve your workround so that it behaves as you want.
我将专注于回答如何解决您的工作,以便它按照您的意愿行事。
To capture actions performed on one Activity within another requires three steps.
要捕获在另一个活动中对一个活动执行的操作需要三个步骤。
Launch the secondary Activity (your 'camera Activity') as a subactivity by using startActivityForResult
instead of startActivity
.
使用startActivityForResult
代替将辅助活动(您的“相机活动”)作为子活动启动startActivity
。
Intent i = new Intent(this,CameraActivity.class);
startActivityForResult(i, STATIC_INTEGER_VALUE);
Within the subactivity (camera Activity), rather than just closing the Activity when a user clicks the different tab image, you need to create a new Intent and include the index of the tab to display when you return to the parent app using the extras bundle. To pass it back to the parent call setResult
before calling finish
to close the camera Activity.
在子活动(相机活动)中,您需要创建一个新的 Intent,并在您使用 extras 包返回父应用程序时包含要显示的选项卡索引,而不仅仅是在用户单击不同的选项卡图像时关闭该活动. setResult
在调用finish
关闭相机活动之前将其传递回父调用。
resultIntent = new Intent(null);
resultIntent.putExtra(PUBLIC_STATIC_STRING_IDENTIFIER, tabIndexValue);
setResult(Activity.RESULT_OK, resultIntent);
finish();
The final step is in the calling Activity, override onActivityResult
to listen for callbacks from the camera Activity. Get the extra from the returned Intent to determine the index of the tab you should be displaying.
最后一步是在调用 Activity 中,覆盖onActivityResult
以侦听来自相机 Activity 的回调。从返回的 Intent 中获取额外信息,以确定您应该显示的选项卡的索引。
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode) {
case (STATIC_INTEGER_VALUE) : {
if (resultCode == Activity.RESULT_OK) {
int tabIndex = data.getIntExtra(PUBLIC_STATIC_STRING_IDENTIFIER);
// TODO Switch tabs using the index.
}
break;
}
}
}