OnActivityResult在TabActivityGroup中不起作用?

时间:2012-05-27 20:47:48

标签: android android-activity android-camera android-tabhost tabactivity

我使用Tab Activity作为主Activity,它有4个标签。一个选项卡是活动组,有三个按钮。每个按钮是一个活动。我在其中一个活动中使用相机。我知道它的工作原因是因为我已经在存储卡中检查了图像被保存,但它没有调用Onactivityresult()方法。

这是为了显示ContentView

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    View viewToLoad = LayoutInflater.from(this.getParent()).inflate(R.layout.enter_expenses, null);
    this.setContentView(viewToLoad);            

此代码用于拍摄图像

             dbimgguid = UUID.randomUUID();
             imagename =dbimgguid.toString();
             Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
             photo = new File(Environment.getExternalStorageDirectory(), "Expenses"+imagename+".jpg");
             intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(photo));
             imageurl = Uri.fromFile(photo); 
             startActivityForResult(intent, CAMERA_RECEIPTREQUEST); 

这是活动结果

     @Override
     protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
     super.onActivityResult(requestCode, resultCode, data);  
     System.out.println(requestCode);
     System.out.println(resultCode);
     switch(requestCode)
     {
     case CAMERA_RECEIPTREQUEST:  
         if(resultCode== Activity.RESULT_OK)
         {
         //Toast.makeText(this, "Receipt Image Saved", Toast.LENGTH_SHORT).show();
         BitmapFactory.Options options = new BitmapFactory.Options();
         options.inSampleSize = 8;
         //ImageView jpgView = (ImageView)findViewById(R.id.imageView1);
         Bitmap receipt = BitmapFactory.decodeFile(photo.toString(),options);  
         String image = "Expenses"+imagename+".jpg"; 
         Intent imagepass = new Intent(ExpensesActivity.this,ReviewReceiptImage.class);
         imagepass.putExtra("receipt", receipt);
         imagepass.putExtra("imagename", image);
         startActivityForResult(imagepass, CAMERA_CONFIRMRECEIPT); 
         Toast.makeText(this, "Receipt Image Saved", Toast.LENGTH_SHORT).show();
         }

     case CAMERA_CONFIRMRECEIPT:

         Toast.makeText(this, "Receipt Image Saved", Toast.LENGTH_SHORT).show();
         if(resultCode == CAMERA_CONFIRMRECEIPT)
         {
             take_receipt.setEnabled(false);
             take_receipt.setVisibility(View.GONE);
             show_receipt.setEnabled(true);
             show_receipt.setVisibility(View.VISIBLE);
         } 
         break;
     }
 }

感谢您的帮助

1 个答案:

答案 0 :(得分:2)

我已经解决了我的问题。请在下面看到我的答案,

我终于弄清楚/记得这个问题是因为Android只允许嵌套的子活动层......即子活动不能嵌套子活动。要解决这个问题:

从您的子活动中拨打getParent().startActivityForResult() 您的父(活动组)将能够处理onActivityResult。所以我创建了一个ActivityGroup的子类并处理了这个onActivityResult。 如果需要,您可以将结果重新路由回子活动。只需按getLocalActivityManager().getCurrentActivity()获取当前活动即可。我的子活动继承自定义活动,因此我在该子类中添加了handleActivityResult(requestCode, resultCode, data)以供ActivityGroup调用。

使用确切代码回答上述问题

<强> ActivityGroup-&GT; SubActivities-&GT; onActivityResult-&GT;的ActivityGroup(onActivityResult) - &GT;子活动(onActivityResult)

这是ActivityGroup类,您必须在其中为所有子活动定义onActivityResult,

@Override
public void onActivityResult(int requestCode,int resultCode, Intent data)
{

         switch(requestCode)
            {

            case CAMERA_RECEIPTREQUEST:   
            if(resultCode == Activity.RESULT_OK)
            {
            ExpensesActivity activity = (ExpensesActivity) getLocalActivityManager().getCurrentActivity();
            activity.onActivityResult(requestCode, resultCode, data);
            }
            break;

            case CAMERA_CONFIRMRECEIPT:
            ExpensesActivity activity1 = (ExpensesActivity) getLocalActivityManager().getCurrentActivity();
            activity1.onActivityResult(requestCode, resultCode, data);   
            }

        }

在“子活动”中,您必须将视图设置如下

    View viewToLoad = LayoutInflater.from(this.getParent()).inflate(R.layout.enter_time, null);
    this.setContentView(viewToLoad);

然后在Sub-Activity onActivityResulty()

    @Override
    public void onActivityResult(int requestCode,int resultCode, Intent data)
    {
     //Some Code
    }