0

Scenario:

MainActivity (we'll call it activity A) has 3 fragments and starts activity B. Activity B then startActivityForResult (activity C). Problem is when setting result OK on activity C and calling onBackPress, startActivityForResult is getting called on Activity B but activity B is then closed after a few seconds.

Activity A -> Activity B -> Activity C (for Result), Result OK, finish

Then -> Activity B onActivityResult is called with Result OK but activity B finished and return to ActivityA which reload twice (I used Logcat and saw that onDestroy and onCreate was getting called twice for both Activity B and Activity A).

Find the following but didn't helped me a lot Activity is closed after onActivityResult is called

Tried to look at launchMode

Here is Manifest:

<activity
            android:name=".activities.ActivityA"
            android:screenOrientation="portrait"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:theme="@style/AppTheme.NoActionBar"
            android:windowSoftInputMode="stateHidden">
    </activity>

    <activity
            android:name=".activities.ActivityB"
            android:screenOrientation="portrait"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:theme="@style/AppTheme.NoActionBar.Profile">
    </activity>

    <activity
            android:name=".activities.ActivityC"
            android:screenOrientation="portrait"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:theme="@style/AppTheme.NoActionBar.Profile"
            android:windowSoftInputMode="stateHidden|adjustPan">
    </activity>

Here's the launches:

Activity A to Activity B:

intent = Intent(this@ActivityA, ActivityB::class.java)
            intent.putExtra("user", user)
            startActivity(intent)

Activity B to Activity C:

val intent = Intent(this@ActivityB, ActivityC::class.java)
    intent.putExtra("user", user)

Activity C setting result OK and finishing:

setResult(Activity.RESULT_OK)
finish()

Screencast of the behavior: https://youtu.be/gMlH5iujoh0

Any help appreciated

Edit: Activity B onActivityResult

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)

    if (requestCode == REQUEST_CODE_EDIT_PROFILE && resultCode == Activity.RESULT_OK) {
        Timber.e("REQUEST_CODE_EDIT_PROFILE OK AND RESULT OK")
        HelperTools(applicationContext).toastStatus(
            getString(R.string.your_profile_has_been_updated),
            Toast.LENGTH_SHORT,
            ToastType.SUCCESS
        )
        populateData(user)
    }
}

1 Answers1

0
val intent = Intent(this@ActivityB, ActivityC::class.java)
startActivityForResult(Intent(this@ActivityB,ActivityC::class.java),REQUEST_CODE_EDIT_PROFILE ))

Please confirm that data is passed and received correctly by log, and also find what is the cause of onDestroy

Below sample code for getting parceble data

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
        case REQUEST_CODE_CONTACT_SELECTION:
            if (resultCode == Activity.RESULT_OK) {
                assert data != null;
                ContactModel mContactModel = data.getParcelableExtra("contactResult");

                new MessageDialog(this)
                        .setMessage(getString(R.string.are_you_sure_you_want_to_block_contact, mContactModel.getFullName()))
                        .setPositiveButton(getString(R.string.yes), (dialog, which) -> {
                            dialog.dismiss();
                            API_blockUnblockContact(mContactModel, true);
                        })
                        .setNegativeButton(getString(R.string.no), (dialog, which) -> dialog.dismiss()).show();
            }
            if (resultCode == Activity.RESULT_CANCELED) {
                //Write your code if there's no result
                Logger.e(TAG, "Result Canceled");
            }
            break;
    }
}
Michael Yaworski
  • 13,410
  • 19
  • 69
  • 97
Richa Shah
  • 930
  • 3
  • 12
  • 27
  • Data parsing is okay as User in independent of Activity C (already check). I tried to set android:parentActivityName=".activities.ActivityA" (For Activity B) and android:parentActivityName=".activities.ActivityB" for Activity C. Activity B now is NOT destroyed anymore but the onActivityResult of Activity B still redirect to Activity A (which is re-created) – Mobile First Solutions Mar 04 '19 at 05:22
  • Are you calling finish on Activty B? ,Because i have same functionality like yours , i am not using android:parentActivityName yet its working fine – Richa Shah Mar 04 '19 at 08:00
  • I ended up calling finish() after setting result is Activity C instead of onBackPressed() but the behavior is the same. I guess problem is I'm doing some computation in Activity B. To be more clear: After finishing Activity C, I need to "repopulate" data on Activity B. When I dont, it works fine, meaning that there is not redirection to Activity A, But when I call the repopulation in onActivityResult of Activity B, I'm redirected to Activity A – Mobile First Solutions Mar 04 '19 at 16:13
  • what i don't get is why you have put your populateData(user) method outside of if (requestcode == REQUEST_CODE_EDIT_PROFILE && resultCode == Activity.RESULT_OK)? – Richa Shah Mar 05 '19 at 04:56
  • The populateData(user) is indeed inside the curly brackets **{ }** of _(requestcode == REQUEST_CODE_EDIT_PROFILE && resultCode == Activity.RESULT_OK)_, the parentheses **( )** are for the HelperTools method – Mobile First Solutions Mar 05 '19 at 07:32