How to return a result (startActivityForResult) from a TabHost Activity?

后端 未结 5 1732
悲哀的现实
悲哀的现实 2020-11-22 12:40

I have 3 classes in my example: Class A, the main activity. Class A calls a startActivityForResult:

Intent intent = new Intent(this, ClassB.class);
startAct         


        
相关标签:
5条回答
  • 2020-11-22 13:15

    Intent.FLAG_ACTIVITY_FORWARD_RESULT?

    If set and this intent is being used to launch a new activity from an existing one, then the reply target of the existing activity will be transfered to the new activity.

    0 讨论(0)
  • 2020-11-22 13:17

    For start Activity 2 from Activity 1 and get result, you could use startActivityForResult and implement onActivityResult in Activity 1 and use setResult in Activity2.

    Intent intent = new Intent(this, Activity2.class);
    intent.putExtra(NUMERO1, numero1);
    intent.putExtra(NUMERO2, numero2);
    //startActivity(intent);
    startActivityForResult(intent, MI_REQUEST_CODE);
    
    0 讨论(0)
  • 2020-11-22 13:21

    http://tylenoly.wordpress.com/2010/10/27/how-to-finish-activity-with-results/

    With a slight modification for "param_result"

    /* Start Activity */
    public void onClick(View v) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setClassName("com.thinoo.ActivityTest", "com.thinoo.ActivityTest.NewActivity");
        startActivityForResult(intent,90);
    }
    /* Called when the second activity's finished */
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch(requestCode) {
        case 90:
            if (resultCode == RESULT_OK) {
                Bundle res = data.getExtras();
                String result = res.getString("param_result");
                Log.d("FIRST", "result:"+result);
            }
            break;
        }
    }
    
    private void finishWithResult()
    {
        Bundle conData = new Bundle();
        conData.putString("param_result", "Thanks Thanks");
        Intent intent = new Intent();
        intent.putExtras(conData);
        setResult(RESULT_OK, intent);
        finish();
    }
    
    0 讨论(0)
  • 2020-11-22 13:32

    Oh, god! After spending several hours and downloading the Android sources, I have finally come to a solution.

    If you look at the Activity class, you will see, that finish() method only sends back the result if there is a mParent property set to null. Otherwise the result is lost.

    public void finish() {
        if (mParent == null) {
            int resultCode;
            Intent resultData;
            synchronized (this) {
                resultCode = mResultCode;
                resultData = mResultData;
            }
            if (Config.LOGV) Log.v(TAG, "Finishing self: token=" + mToken);
            try {
                if (ActivityManagerNative.getDefault()
                    .finishActivity(mToken, resultCode, resultData)) {
                    mFinished = true;
                }
            } catch (RemoteException e) {
                // Empty
            }
        } else {
            mParent.finishFromChild(this);
        }
    }
    

    So my solution is to set result to the parent activity if present, like that:

    Intent data = new Intent();
     [...]
    if (getParent() == null) {
        setResult(Activity.RESULT_OK, data);
    } else {
        getParent().setResult(Activity.RESULT_OK, data);
    }
    finish();
    

    I hope that will be helpful if someone looks for this problem workaround again.

    0 讨论(0)
  • 2020-11-22 13:32

    You could implement a onActivityResult in Class B as well and launch Class C using startActivityForResult. Once you get the result in Class B then set the result there (for Class A) based on the result from Class C. I haven't tried this out but I think this should work.

    Another thing to look out for is that Activity A should not be a singleInstance activity. For startActivityForResult to work your Class B needs to be a sub activity to Activity A and that is not possible in a single instance activity, the new Activity (Class B) starts in a new task.

    0 讨论(0)
提交回复
热议问题