onActivityResult被立即叫 [英] onActivityResult being called instantly

查看:143
本文介绍了onActivityResult被立即叫的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我启动使用startActivityForResult()从一个按钮处理程序,和我的onActivityResult()的活动被称为的的onCreate()为目标的活动被击中瞬间,甚至之前。

I'm launching an activity using startActivityForResult() from a button handler, and my onActivityResult() is being called instantly, even before the onCreate() for the target activity is being hit.

public void onGraphNext (View target) {
    Intent i = new Intent(this, AddCommentActivity.class);
    startActivityForResult(i,6);    // 6 is arbitrary request code               
}    

。 。 。

. . .

protected void onActivityResult(int requestCode, int resultCode,
        Intent returnData) {
   if  ( (resultCode == RESULT_OK) && (requestCode == 6) ) {
   Bundle extras = returnData.getExtras();
   comment = extras.getString("comment");
     }
} 

结果code返回为0,并且请求code是6。其他方面计算器我见过的人将此问题报告和解决方案是不使用singeInstance在清单中的launchMode。但我使用的标准。 。 。

The result code returned is 0 and the request code is 6. Elsewhere on StackOverflow I've seen people report this problem and the solution was to not use singeInstance for the launchMode in the manifest. But I'm using standard . . .

<activity android:name="AddCommentActivity"
          android:configChanges="orientation"
          android:screenOrientation="portrait"
          android:launchMode="standard"></activity>  

在此先感谢任何见解!

Thanks in advance for any insights!

编辑:我做了一个简单的测试程序,我可以可靠地重现该问题时,主叫方(发射器) - 与onActivityResult活动 - 是被调用的singleInstance和活动( launchee)为标准。即,

I made a simple test program and I can reproduce the problem reliably when the caller ("launcher") - the activity with the onActivityResult - is a singleInstance and the Activity being invoked ("launchee") is standard. i.e.,

<activity android:name="Launcher"
          android:screenOrientation="portrait"
          android:launchMode="singleInstance"></activity> 

<activity android:name="Launchee"
          android:screenOrientation="portrait"
          android:launchMode="standard"></activity>  

这对我来说是一个问题,因为在真正的应用程序,被叫必须是singleInstance其他原因,但它希望有按钮启动等活动,要求用户输入。要不然怎么做,如果我不能使用startActivityForResult?

This is a problem for me because in the real app, the called must be a singleInstance for other reasons, but it wants to have buttons to start up other activities to request user input. How else to do this if I can't use startActivityForResult?

推荐答案

您无法使用 startActivityForResult()如果活动被启动不是在相同的任务运行活动启动它。这意味着,无论是活动可以有 launchMode =singleInstance。如果您需要在不同的任务中运行这两个活动,则需要使用不同的机制。该<一href="http://developer.android.com/reference/android/app/Activity.html#startActivityForResult%28android.content.Intent,%20int,%20android.os.Bundle%29">documentation为 startActivityForResult() 八九不离十,有点暗示了这一点:

You cannot use startActivityForResult() if the activity being started is not running in the same task as the activity that starts it. This means that neither of the activities can have launchMode="singleInstance". If you require that these 2 activities run in different tasks, then you need to use a different mechanism. The documentation for startActivityForResult() sorta-kinda hints at this:

举个例子,如果你正在开展活动使用的singleTask   发射方式,它不会在你的任务运行,因此你会   立即收到取消的结果。

"For example, if the activity you are launching uses the singleTask launch mode, it will not run in your task and thus you will immediately receive a cancel result."

你可以做的是简单地使用启动活动 startActivity()并有所谓的活性呼叫 startActivity()返回到你的活动,发回数据,需要在额外的意图它使用。

What you can do is to simply start the activity using startActivity() and have the called activity call startActivity() to return to your activity, sending back data as necessary as extras in the Intent it uses.

不过,你可能会考虑是否真的需要这些特殊launchModes。一般他们只需要为家庭屏替换等非常特殊的应用。谁使用大多数开发人员 singleInstance singleTask 启动模式被错误地使用它们。

However, you might consider whether you really need these special launchModes. In general they are only necessary for HOME-screen replacements and other very special applications. Most developers who use singleInstance or singleTask launch modes are using them incorrectly.

这篇关于onActivityResult被立即叫的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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