Android onActivityResult 从未调用过 [英] Android onActivityResult NEVER called

查看:29
本文介绍了Android onActivityResult 从未调用过的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 onActivityResult 方法从未被调用.我使用的是安卓 2.2

my onActivityResult method is never called. am using android 2.2

我使用的是 Tabhost,其中 TabHosts 包含 TabGroups,其中 TabGroups 包含单个活动.

I am using a Tabhost, where TabHosts contain TabGroups which contain individual Activities.

我的一项个人活动运行以下意图

One of my individual activity runs the following intent

 Intent intent = new Intent(); 
 intent.setType("image/*");
 intent.setAction(Intent.ACTION_GET_CONTENT);
 startActivityForResult(Intent.createChooser(intent,
                    "Select Picture"), 0);

这会加载我的图库应用,我使用默认的安卓图库来选择一张图片,当我返回时,我的 onActivityResult 不会被称为我的活动.

this loads my gallery apps, I use the default android gallery to select one image and when I return my onActivityResult is not called my activity.

它看起来像这样 - 我在 if(resultCode == 0) 处放置了一个断点,所以现在,我的 onActivityResult 的逻辑应该无关紧要

It looks like this - and I put a breakpoint at if(resultCode == 0) , so right now, the logic of my onActivityResult should not matter

 public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == 0) {
        if (requestCode == 0) {
            Uri selectedImageUri = data.getData();

            //OI FILE Manager
            filemanagerstring = selectedImageUri.getPath();

            //MEDIA GALLERY
            selectedImagePath = getPath(selectedImageUri);

            //DEBUG PURPOSE - you can delete this if you want
            if(selectedImagePath!=null)
                System.out.println(selectedImagePath);
            else System.out.println("selectedImagePath is null");
            if(filemanagerstring!=null)
                System.out.println(filemanagerstring);
            else System.out.println("filemanagerstring is null");

            //NOW WE HAVE OUR WANTED STRING
            if(selectedImagePath!=null)
                System.out.println("selectedImagePath is the right one for you!");
            else
                System.out.println("filemanagerstring is the right one for you!");
        }
    }
}

对于 tabhost/tabgroup 中的活动,生命周期函数经常被无序和间歇性地调用,所以我检查了在画廊关闭后正在调用哪些生命周期函数(这发生在我从 android 画廊中选择一个图像时)

Lifecycle functions are often called out of order and intermittently for Activities within a tabhost/tabgroup, so I checked to see what lifecycle functions ARE being called after the gallery closes (this happens as soon as I select an image from the android gallery)

唯一被调用的是 TabHost 活动中的 onResume().所以我尝试将完全相同的 onActivityResult() 方法放在我的 TabHost 类以及 TabActivity 类中.在方法开始处的同一位置有一个断点.

The only one being called is the onResume() in my TabHost activity. So I tried putting the exact same onActivityResult() method in my TabHost class AS WELL AS the TabActivity class. With a breakpoint in the same location at the beginning of method.

这些类都没有被调用.

我现在正在画一个空白,如果没有内置的接收方法会响应它,我如何从我的应用程序中的图库应用程序中获取结果.

I'm drawing a blank now, how can I get the result from the gallery app in my app if none of the built in receiving methods will respond to it.

因为我知道我的主 TabHost 调用了 onResume(),所以我尝试添加 Intent graphics = getIntent(); 以查看它是否会从画廊选择,它没有,所以我也不知道如何在 onResume() 方法中执行逻辑.

Since I know that my main TabHost gets the onResume() called, I tried added Intent graphics = getIntent(); to see if it would receive data from the gallery selection, it does not, so I don't see how I can do the logic in the onResume() method either.

欢迎提供解决方案!:)

Solutions welcome! :)

推荐答案

解决方案是在主活动之上调用一个透明的活动.这个透明的Activity在tabhost前面,会有正常的生命周期功能.

The solution is to call a transparent activity over top of the main activity. This transparent activity is in front of the tabhost and will have normal lifecycle functions.

这个透明的 Activity 调用画廊意图 onCreate(),它在其 onActivityResult 中像正常一样返回所有内容,您将能够像正常一样将返回的信息传递回应用程序的其余部分.Finish() 位于 onActivityResult 方法内部,因此用户甚至不会注意到调用了透明 Activity.

This transparent activity calls the gallery intent onCreate(), it gets everything returned like normal in its onActivityResult and you will be able to pass the information returned back to the rest of the app like normal. finish() is inside of the onActivityResult method, so the user never even notices that a transparent activity was called.

从评论中复制的更新:

活动 A 通过正常意图调用活动 B.Activity B 没有 xml 并像这样运行 onCreate

Activity A calls Activity B via normal intent. Activity B has no xml and runs onCreate like this

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    //setContentView(R.layout.dialogpopper); 

    Intent intent = new Intent(Intent.ACTION_PICK); 
    intent.setType("image/*"); startActivityForResult(intent, 0);

}//end onCreate 

当 Activity C 完成时,它会调用 Activity B 的 onActivityResult

and when Activity C is finished it calls the onActivityResult of Activity B

这篇关于Android onActivityResult 从未调用过的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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