没有调用onActivityResult?

时间:2016-08-13 03:50:36

标签: android android-activity onactivityresult

我从ProfileActivity工具栏开始HomeActivity。 出于某种原因,当我从ProfileActivity返回时,onActivityResult()未被调用。以下是相关代码。提前谢谢!

HomeActivity内的工具栏代码:

   @Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.searchBox:

            return true;

        case R.id.userProfile:
            Intent intent = new Intent(HomeActivity.this, ProfileActivity.class);
            Bundle b = new Bundle();
            b.putString("userID", uID);
            intent.putExtras(b);
            startActivityForResult(intent, 10);
            finish();
            return true;

        default:
            return super.onOptionsItemSelected(item);

    }
}
onActivityResult()内的

HomeActivity

    @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == 10) {
        Log.d("WorkoutLog", "onresult called");
        if(resultCode == Activity.RESULT_OK){
            Toast.makeText(context, data.getStringExtra("date"), Toast.LENGTH_SHORT).show();
        }
    }
}

ProfileActivity中调用的方法返回主页:

    public void editWorkout(String date){

    Intent intent = new Intent();
    intent.putExtra("date", date);
    setResult(Activity.RESULT_OK, intent);
    finish();

}

HomeActivityProfileActivity清单:

        <activity
        android:name=".HomeActivity"
        android:screenOrientation="portrait"
        android:windowSoftInputMode="adjustPan">
        <intent-filter>
            <action android:name="android.intent.action.HOME" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <activity
        android:name=".ProfileActivity"
        android:screenOrientation="portrait"
        android:windowSoftInputMode="adjustPan">
        <intent-filter>
            <action android:name="android.intent.action.PROFILE" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

1 个答案:

答案 0 :(得分:3)

finish()有效杀死了调用Activity,请勿在{{1​​}}之后调用它:

startActivityForResult()