Android 在选项卡中的嵌套活动中使用 startActivityForResult。

2024-01-05

我正在编写一个应用程序,该应用程序由在 tabhost 中创建的多个选项卡组成:

intent = new Intent().setClass(this, Home.class);
    spec = tabHost.newTabSpec("Home").setIndicator("Home",
                      res.getDrawable(R.drawable.home))
                  .setContent(intent);
    tabHost.addTab(spec);

在相关选项卡中,我使用 ActivityGroup 更改选项卡中的不同活动:

Intent intent = new Intent(Info1.this, Enroll2.class);
            intent.putExtra("info", Info);

            View newView = Group.group.getLocalActivityManager().startActivity("Info1", intent
                    .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))  
                    .getDecorView();  
            Group.group.replaceView(newView);

在其中一项活动中,我需要拍照,并且我尝试使用设备上的默认相机应用程序:

//define the file-name to save photo taken by Camera activity
    String fileName = "picture" + Integer.toString(pictureCount);

    //create parameters for Intent with filename
    ContentValues values = new ContentValues();
    values.put(MediaStore.Images.Media.TITLE, fileName);
    values.put(MediaStore.Images.Media.DESCRIPTION,"Image capture by camera");

    //imageUri is the current activity attribute, define and save it for later usage (also in onSaveInstanceState)
    imageUri = getContentResolver().insert(
    MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);

    //create new Intent
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
    intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);

    startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);

这样可以正确启动相机应用程序,但是拍照后它不会进入 onActivityResult 方法。我尝试将此方法放置在选项卡链中的每个类中,但它不会在其中任何一个类中输入代码。

我注意到这个问题之前被问过如何在 TabHost 的选项卡子项中启动活动以获得结果 https://stackoverflow.com/questions/4529160/how-to-startactivityforresult-in-tab-child-of-tabhost但唯一可能有用的答案是重定向到如何从 TabHost Activity 返回结果 (startActivityForResult)? https://stackoverflow.com/questions/2497205/how-to-return-a-result-startactivityforresult-from-a-tabhost-activity这是一个关于从基本活动中使用 startActivityForResult 来启动 tabActivity 的问题,而不是从 tabActivity 启动活动,所以它没有用。

我还经常看到人们说,当您使用 ActivityGroup 时,这不起作用,但没有人建议还有其他方法可以做到这一点。

任何帮助,将不胜感激。


好吧,我找到了解决这个问题的方法。

首先,我创建了另一个活动,我使用基本的 startActivity() 调用开始该活动,我将其称为结果控制器。这不会将任何数据传递回选项卡式活动,这意味着您不必担心它的去向。

其次,我创建了一个简单的静态数据类,我将其称为 DataConnector。 ResultController 将获取 DataConnector 的实例并在那里插入数据

然后,在原始活动(在选项卡中)中,我实现了 onWindowFocusChanged 方法来确定用户何时返回到它。我获得了 DataConnector 的实例,并且能够从那里提取我需要的数据。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android 在选项卡中的嵌套活动中使用 startActivityForResult。 的相关文章

随机推荐