简体   繁体   中英

onActivityResult not being called?

I am starting ProfileActivity from HomeActivity 's toolbar. For some reason when I return from ProfileActivity , the onActivityResult() isn't being called. Below are the relevant code. Thanks in advance!

Toolbar code inside 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() inside 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();
        }
    }
}

Method called in ProfileActivity to return back to home:

    public void editWorkout(String date){

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

}

HomeActivity and ProfileActivity manifest:

        <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>

finish() effectively kills the calling Activity , do not call it after startActivityForResult() :

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);

    return true;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM