Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rare NPE if presenter used in onActivityResult #238

Open
asfdfdfd opened this issue Dec 13, 2018 · 4 comments
Open

Rare NPE if presenter used in onActivityResult #238

asfdfdfd opened this issue Dec 13, 2018 · 4 comments

Comments

@asfdfdfd
Copy link

asfdfdfd commented Dec 13, 2018

implementation "com.arello-mobile:moxy:1.5.5"
implementation "com.arello-mobile:moxy-android:1.5.5"
kapt "com.arello-mobile:moxy-compiler:1.5.5"

This is pretty rare crash and i could not reproduce it on my devices.

Crashlytics reports — http://crashes.to/s/4a9639b6eba, http://crashes.to/s/a2bb8339d24
Crashlytics report sample — com.mirrorai.app_issue_1947_crash_5C115CBA03CC000111718A8B503A537C_DNE_0_v2.txt

I have several fragments that receives results via onActivityResult. Let's take photo gallery for example:

class TakingPhotoFragment : MirrorFragment(), TakingPhotoView {

    @InjectPresenter
    lateinit var presenter: TakingPhotoPresenter

    @ProvidePresenter
    fun providePresenter() = TakingPhotoPresenter()

and a bit later in this class

    override fun takePhotoFromGallery() {
        val intent = Intent()
        intent.type = "image/*"
        intent.action = Intent.ACTION_GET_CONTENT

        startActivityForResult(Intent.createChooser(intent, "Select Picture"), REQUEST_CODE_PICK_IMAGE)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        if (requestCode == REQUEST_CODE_PICK_IMAGE) {
            if (resultCode == Activity.RESULT_OK) {
                if (data != null) {
                    presenter.uploadPhotoFromGallery(data)
                }
            }
        }
    }

presenter in uploadPhotoFromGallery sometimes (rarely but noticeable) is null and causes NPE.

I use custom base fragment MvpAndroidXFragment that is just Kotlin clone of MvpAppCompatFragment for AndroidX — https://gist.github.com/asfdfdfd/87552746c9182f6b7311f3d9ce6272a6.

But this issue has appeared before migrating to Android X.

@senneco
Copy link
Collaborator

senneco commented Dec 14, 2018

It seems that onActivityResult called before onCreate. But this shouldn't. Could you debug please, that onCreate called before onActivityResult or not?

@asfdfdfd
Copy link
Author

Okay. I'll add logs to the onCreate and onActivityResult of base fragment.

@asfdfdfd
Copy link
Author

It will be something like this.

abstract class MirrorFragment : MvpAndroidXFragment(), OnBackPressedListener {

    private var isOnCreateCalledBeforeSuper = false
    private var isOnCreateCalledAfterSuper = false

    override fun onCreate(savedInstanceState: Bundle?) {
        isOnCreateCalledBeforeSuper = true

        super.onCreate(savedInstanceState)

        isOnCreateCalledAfterSuper = true
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        if (!isOnCreateCalledBeforeSuper || !isOnCreateCalledAfterSuper) {
            Timber.e("onCreate is not called: %b, %b.", isOnCreateCalledBeforeSuper, isOnCreateCalledAfterSuper)
        }

        super.onActivityResult(requestCode, resultCode, data)
    }
}

@asfdfdfd
Copy link
Author

This code was in production for a while and i never had logs in Crashlytics about "onCreate is not called". So at least in this case onCreate is seems to be called.

I could check other things if you have any ideas.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants