Android的onActivityResult叫早 [英] Android onActivityResult called early
问题描述
我有2个活动,每个单独的应用程序。活动1有一个按钮,用户可以点击并使用在其的onClick()
方法的意图调用第二个活动:
I have 2 Activities, each in seperate applications. Activity1 has a button the user can click and it calls the second activity using an intent in its onClick()
method:
Intent myIntent = getPackageManager().getLaunchIntentForPackage(com.myProject.Activity2);
startActivityForResult(myIntent, 600);
这正常启动活性2,从活动1,但 onActivityResult
被称为活性1前的onCreate
被称为活性2,而不是在 onBack pressed()
在这里我设置了回报意图。
This correctly launches Activity2 from Activity1, but onActivityResult
gets called in Activity1 before onCreate
gets called in Activity2, instead of in onBackPressed()
where I set up the return intent.
下面是的onCreate
法活性2:
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
下面是 onBack pressed
方法的活性2当前版本:
Here is the current version of onBackPressed
method for Activity2:
@Override
public void onBackPressed() {
Intent intent = new Intent();
intent.putExtra("Stuff", someStuff);
if(getParent()==null){
setResult(Activity.RESULT_OK, intent);
}else{
getParent().setResult(Activity.RESULT_OK, intent);
}
finish();
super.onBackPressed();
}
我的AndroidManifest.xml中有以下意图过滤器的活性2:
My AndroidManifest.xml has the following intent filter for Activity2:
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
我验证了我的 launchMode
是标准
(而不是 singleTask
等)的建议这里和我的要求,code不是负作为警告<一个href="http://stackoverflow.com/questions/5289726/onactivityresult-not-being-called-when-listactivity-finishes">here.我也试过机器人:launchMode =singleTop
,但是这是一个不走也
I verified that my launchMode
is standard
(and not singleTask
, etc) as advised here and my request code is not negative as warned here. I also tried android:launchMode="singleTop"
, but that was a no-go also.
我也试过不叫完成()
在 onBack pressed()
的活性2所提到< A HREF =http://stackoverflow.com/questions/6742438/onactivityresult-not-called-in-android>此处(也只有 super.onBack pressed( )
的建议<一href="http://stackoverflow.com/questions/6322874/onactivityresult-has-intent-data-as-null-after-an-activity-has-finished">here)并再次呼吁其作为建议<一href="http://groups.google.com/group/android-developers/browse_thread/thread/b3b2ad819ee40eff#">here.
I also tried not calling finish()
in onBackPressed()
for Activity2 as mentioned here (also with just super.onBackPressed()
as suggested here) and again calling it as suggested here.
此外我试着注释掉行 intent.putExtra(东西,someStuff);
,因为它似乎导致了的this人。
Additionally I tried commenting out the line intent.putExtra("Stuff", someStuff);
as it seemed to cause trouble for this person.
任何想法,我可能是做错了?
Any ideas as to what I might be doing wrong?
推荐答案
因此,这里是去照料它的最终解决方案:
So here is the final solution that took care of it:
我改变了意图活性1以下内容:
I changed the intent for Activity1 to the following:
Intent myIntent = new Intent();
myIntent.setClassName("com.myProject", "com.myProject.Activity2");
startActivityForResult(myIntent, 600);
由于某种原因,Android的要求对除由第一个参数给出的包名,第二个参数的完全合格的名称。现在它的作品! :)
For some reason Android requires the fully qualified name for the second parameter in addition to the package name given by the first parameter. Now it works! :)
这篇关于Android的onActivityResult叫早的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!