0

I have an existing application. Now I want to add some extra functionality into it. The extra functionality require some library. If i use the library in my app, the application size increases. So I want to make this extra functionality in a separate app and then integrate the new app to my existing app ( If i have some update for the existing app, then I have to download the whole app, which I don't want). I don't want both in one apk. Both should be standalone. From First application let say A, I want to call an activity in the new application B. I used intent.setComponent() and the activity of application B is called from A but I cannot get the result from Application B back in the first application.

Application A

public void onClickBtnToApplicationB(View v) {
    try {
        final Intent intent = new Intent(Intent.ACTION_MAIN, null);
        final ComponentName cn = new ComponentName("pakacagename","package.class");
        intent.setComponent(cn);
        intent.setAction(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);                 
        startActivityForResult(intent, REQUEST_CODE);
    } catch (ActivityNotFoundException e) {
    //handle Exception
    } 
}

public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    switch (requestCode) {
        case REQUEST_CODE:
           handleResult(resultCode, intent);
           break;
    }
}

public void handleResult(int resultCode, Intent intentResult) {
    switch (resultCode) {
        case RESULT_OK:
            String Result = intentResult.getStringExtra("RESULT");
            // I need Results from Application B here..
            break;

         case RESULT_CANCELED:
            break;
    }
  }

Application B: In application B, class 1 starts another activity for Result 2.class and get the results. After the results are received they are send back to the first application in handle result method'a putextras. So I need to get result in Application A's handleresult method which I didn't get.

Intent s = new Intent(1.this,2.class);
startActivityForResult(s, REQUEST_CODE_B);
protected void onActivityResult(int requestCode, int resultCode, Intent intentResult) {     
    switch(requestCode){
        case REQUEST_CODE_B:
            handleResult(resultCode, intentResult);
    }
}

public void handleResult(int resultCode, Intent intentResult) {
    switch (resultCode) {
    case RESULT_OK:
        String scanResult = intentResult.getStringExtra("RESULT");
        Intent newintent = new Intent();
        newintent.putExtra("RESULT", scanResult);
        setResult(Activity.RESULT_OK, newintent);
        finish();
        break;

    case RESULT_CANCELED:
        break;
}
4

2 回答 2

0

2 意图设计您必须将一些数据从当前意图传递给新意图,您可以执行以下操作:

Intent i = new Intent(yourcurrentactivity.this, yournewactivity.class);
                    // following line is an example of passing data to the new intent.
        i.putExtra(getString(R.string.str_selecteddialog), "1");
        startActivity(i);
        this.finish();

在您在 onCreate 中启动的新意图中,您需要从包中检索数据:

Bundle extras = getIntent().getExtras();
    if (extras != null) {
        // Log.i(TAG, " 022:... we got some extras");
        str_selecteddialog = getIntent().getStringExtra(getString(R.string.str_selecteddialog));

显然,您必须将这种类型的代码放入两个意图中并反转活动名称。我还会使用 try 命令来捕捉任何与额外内容不被重视的问题。这个条件必须在一开始就存在。

取决于变量类型,有不同类型的 put 和 get 'Extras'。为了避免混淆,我还将使用一组变量(名称值对,即 Extras)传递给意图 B,并使用另一组变量从意图 B 传递到意图 A。

此外,您将希望每次只运行一个实例,这可以通过两种方式完成。在上述每个活动的清单中,您可以指定:

android:launchMode="singleTop"

或者在开始另一个意图之前把它放在 startActivity 之前:

Intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

这些中的任何一个或两者都将仅在堆栈中保留每个意图中的一个,而不是每个意图的倍数。

此外,以防万一如果您想重新启动当前意图,请参阅重新启动当前意图

希望这可以帮助。

于 2013-07-31T04:39:34.037 回答
0

看起来onActivityResult您的 Activity A 从未被调用过。

我敢打赌,这与您如何称呼活动 B 有关。

而不是这样做:

    final Intent intent = new Intent(Intent.ACTION_MAIN, null);
    final ComponentName cn = new ComponentName("pakacagename","package.class");
    intent.setComponent(cn);
    intent.setAction(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_LAUNCHER);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);                 
    startActivityForResult(intent, REQUEST_CODE);

,你真的应该这样做:

    Intent intent = new Intent(ACTION_B);
    startActivityForResult(intent, REQUEST_CODE);

ACTION_B 是表单的常量"com.mypackage.myapp.ACTION_B",您必须在 B 的清单中声明如下:

    <activity
        android:name="com.yourpackage.b.BActivity"
    >
        <intent-filter>
            <action android:name="com.mypackage.myapp.ACTION_B" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
于 2013-07-31T10:43:51.587 回答