【问题标题】:startActivityForResult() immediately call OnActivityResult when call another appstartActivityForResult() 在调用另一个应用程序时立即调用 OnActivityResult
【发布时间】:2015-05-29 06:41:32
【问题描述】:

我有 2 个应用程序,AB

在应用程序A 中,我想调用应用程序B 并从应用程序B 获取结果。我试试这个代码:

Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.app.B");
startActivityForResult(launchIntent, CODE);

但在我调用该代码后,onActivityResult() 方法立即调用并给出结果RESULT_CANCELLED

应用 B 清单:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.app.B">
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".TestActivity"
        android:label="@string/app_name"
        android:windowSoftInputMode="adjustResize" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>
</manifest>

App B 测试活动:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test);

    if (getCallingActivity() == null) {
        startActivity(new Intent(this, MainActivity.class));
        finish();
    } else {
        Intent returnIntent = new Intent();
        setResult(RESULT_OK, returnIntent);
        finish();
    }
}

【问题讨论】:

    标签: android


    【解决方案1】:

    我只是根据this answerstartActivityForResult()之前添加launchIntent.setFlags(0);

    【讨论】:

      【解决方案2】:

      这就是你编码的目的.. 一旦 B 应用程序的 Activity 启动,您就调用了 setResult 方法,因为它在 onCreate() 方法中。 你得到失败结果代码的原因是因为你已经设置了

      setResult(RESULT_OK, returnIntent)
      

      但你应该使用 Activity.RESULT_OK 之类的

      setResult(Activity.RESULT_OK, returnIntent)
      

      【讨论】:

        【解决方案3】:
        getCallingActivity()
        

        只有在您使用 startActivityForResult Intent 调用特定 Activity 时才不会为空。
        但是您调用的不是具体活动,而是getPackageManager().getLaunchIntentForPackage("com.app.B");
        它检索指定包的默认启动活动,并以默认(空)意图启动它。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-08-27
          • 2012-11-29
          • 2014-05-13
          • 1970-01-01
          • 2014-08-11
          • 1970-01-01
          相关资源
          最近更新 更多