gpt4 book ai didi

android - OnActivityResult 在 TabActivityGroup 中不起作用?

转载 作者:行者123 更新时间:2023-11-29 22:05:58 25 4
gpt4 key购买 nike

我使用 Tab Activity 作为主 Activity,其中有 4 个选项卡。一个选项卡是 Activity Group,它具有三个按钮。每个按钮是一个 Activity 。我在其中一项 Activity 中使用相机。我知道它的工作原理,因为我已经在存储卡中检查了图像是否已保存,但它没有调用 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);

这是 Activity 结果

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

谢谢大家的帮助

最佳答案

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

我终于弄明白/记得这个问题是因为 Android 只允许嵌套子 Activity 层...即子 Activity 不能嵌套子 Activity 。解决这个问题:

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

用准确的代码回答上面的问题

ActivityGroup->SubActivities->onActivityResult->ActivityGroup(onActivityResult)->subActivity(onActivityResult)

这是 ActivityGroup 类,您必须在其中为所有子 Activity 定义 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);
}

}

在子 Activity 中,您必须如下设置 View

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

然后在子Activity的onActivityResulty()

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

关于android - OnActivityResult 在 TabActivityGroup 中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10777154/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com