0
votes

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
Can you please post your activity B onActivityResult logic here,Dhaval Solanki
what is user can you provide source of this class? If it is parcelable/serializable can you make sure if it parses correctly ?RadekJ
@DhavalSolanki I edited post with Activity B onActivityResult, Radekj User is a Parcelable Class and parsed correctly as it populate the data correctlyMobile First Solutions

1 Answers

0
votes
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;
    }
}