Android:捕获活动的返回 [英] Android: Capturing the return of an activity

查看:15
本文介绍了Android:捕获活动的返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于启动新活动的问题.归结为这一点.我有 3 个标签页

I have a question regarding launching new activities. It boils down to this. I have 3 tabs on a view

A) contains gMap activity
 B) camera activity
 C) some random text fields.

要求是应用程序在纵向模式下运行.

Requirement is that the application runs in Portrait mode.

所有 3 个选项卡都按预期工作,但相机预览表面 (B) 除外.它旋转了 90 度.他们唯一使它正确的方法是将应用程序设置为横向,这会抛出我所有的标签,并且几乎无法使用.

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.

我的解决办法是:替换

我的相机活动与常规活动是空的,除了

my camera activity with a regular activity that is empty w/ the exception of

Intent i = new Intent(this,CameraActivity.class);
    startActivity(i);

这会启动我的 CameraActivity.这工作正常.我必须做一个线性布局并包含 3 个看起来像真实标签的图像,所以我可以尝试模仿标签的操作,同时将屏幕旋转到横向并保持视觉效果为纵向.用户可以单击其中一个图像(按钮)以显示下一个选项卡.这是我的问题.它应该退出我的相机活动",返回到选项卡中的空白活动",在那里它应该被解释为从我的图像中单击所需的选项卡.

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.

主要的是,当它返回时,它返回到一个标签下的空白(黑色)页面(因为它是空的").如何将返回事件捕获回调用该活动的页面,然后查看它们执行了哪些操作?

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?

我可以设置一个 onclicklistener,在那里我可以响应被点击的虚假标签(图像)以退出相机活动.退出时,选项卡应更新,以便您返回.有什么建议吗?

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?

谢谢,

推荐答案

我将专注于回答如何解决您的解决方案,使其按照您的意愿运行.

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.

使用 startActivityForResult 而不是 startActivity 将辅助活动(您的相机活动")作为子活动启动.

Launch the secondary Activity (your 'camera Activity') as a subactivity by using startActivityForResult instead of startActivity.

Intent i = new Intent(this,CameraActivity.class);    
startActivityForResult(i, STATIC_INTEGER_VALUE);

在子活动(相机活动)中,您需要创建一个新的 Intent 并包含要在返回父应用程序时显示的选项卡的索引,而不是在用户单击不同的选项卡图像时关闭该活动附加包.在调用 finish 之前将其传递回父调用 setResult 以关闭相机活动.

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.

resultIntent = new Intent(null);
resultIntent.putExtra(PUBLIC_STATIC_STRING_IDENTIFIER, tabIndexValue);
setResult(Activity.RESULT_OK, resultIntent);
finish();

最后一步是在调用 Activity 中,覆盖 onActivityResult 以监听来自相机 Activity 的回调.从返回的 Intent 中获取额外信息,以确定您应该显示的选项卡的索引.

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.

@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; 
    } 
  } 
} 

这篇关于Android:捕获活动的返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆