ASNE icon indicating copy to clipboard operation
ASNE copied to clipboard

[ASNETutorial]Application always crashing after changing orientation (rotating device)

Open ITurchenko opened this issue 10 years ago • 6 comments

Hello again.

How to reproduce bug: Open example project (https://github.com/gorbin/ASNETutorial) on device, rotate device at least once and then press any of the social buttons.

Result for facebook:

Process: com.github.gorbin.asnetutorial, PID: 13770
    java.lang.NullPointerException
            at com.facebook.Session$AuthorizationRequest$1.startActivityForResult(Session.java:1902)
            at com.facebook.Session.tryLoginActivity(Session.java:1409)
            at com.facebook.Session.authorize(Session.java:1159)
            at com.facebook.Session.open(Session.java:1237)
            at com.facebook.Session.openForRead(Session.java:471)
            at com.github.gorbin.asne.facebook.FacebookSocialNetwork.requestLogin(FacebookSocialNetwork.java:149)
            at com.github.gorbin.asne.core.SocialNetwork.requestLogin(SocialNetwork.java:205)
            at com.github.gorbin.asnetutorial.MainFragment$1.onClick(MainFragment.java:160)
            at android.view.View.performClick(View.java:4658)
            at android.view.View$PerformClick.run(View.java:19461)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:146)
            at android.app.ActivityThread.main(ActivityThread.java:5653)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
            at dalvik.system.NativeStart.main(Native Method)

For VKontakte will be something like

Caused by: java.lang.NullPointerException: Argument 'currentActivity' cannot be null
            at com.facebook.internal.Validate.notNull(Validate.java:29)
            at com.facebook.Session.onActivityResult(Session.java:723)
            at com.github.gorbin.asne.facebook.FacebookSocialNetwork.onActivityResult(FacebookSocialNetwork.java:535)
            at com.github.gorbin.asne.core.SocialNetworkManager.onActivityResult(SocialNetworkManager.java:92)
            at ru.**MyPackage***.MyLoginActivity.onActivityResult(LoginActivity.java:75)
            at android.app.Activity.dispatchActivityResult(Activity.java:5734)
            at android.app.ActivityThread.deliverResults(ActivityThread.java:3696)
            at android.app.ActivityThread.handleSendResult(ActivityThread.java:3743)
            at android.app.ActivityThread.access$1400(ActivityThread.java:172)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1356)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:146)
            at android.app.ActivityThread.main(ActivityThread.java:5653)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
            at dalvik.system.NativeStart.main(Native Method)

after finishing fragment.

ITurchenko avatar Nov 04 '14 09:11 ITurchenko

check it, thanks. Forgot about it in ASNETutorial app.

gorbin avatar Nov 05 '14 08:11 gorbin

This looks to be a bug in ASNE rather than just the tutorial. This same exception also occurs when replacing fragments in an activity. For example, say I have a home activity that can open either a login fragment or a signup fragment. If I open the signup fragment, press back, then go to the login fragment, any requestLogin() call will cause the same null pointer.

If it is any help, I've verified this behavior doesn't occur using the Android Social Networks library ASNE was based off.

tylerjroach avatar Dec 17 '14 15:12 tylerjroach

@ITurchenko Here is a workaround that may help for the time being. This worked for me.

Fragment fragment = getSupportFragmentManager().findFragmentByTag(SOCIAL_NETWORK_TAG);
        if(fragment != null) {
            getSupportFragmentManager().beginTransaction().remove(fragment).commit();
            getSupportFragmentManager().executePendingTransactions();
        }

On your orientation change, remove the old asne fragment and create a new one. executePendingTransactions() must be used to make sure the old one is removed before the new created.

Hope this helps.

tylerjroach avatar Dec 17 '14 15:12 tylerjroach

I think the issue is the way that the context is passed.

FacebookSocialNetwork fbNetwork = new FacebookSocialNetwork(this, fbScope);
        mSocialNetworkManager.addSocialNetwork(fbNetwork);

On orientation changed or a replaced fragment, the original "this" fragment context that is passed is stale.

tylerjroach avatar Dec 17 '14 16:12 tylerjroach

Is this bug already fixed?

ultraon avatar Mar 17 '15 07:03 ultraon

I found another way to avoid crash and memory leaks (use this code in your fragment):

...
private SocialNetworkManager snm;
...
@Override
    public void onResume() {
        super.onResume();
        if (null == snm || snm.getInitializedSocialNetworks().isEmpty()) initSocialManager();
    }

    @Override
    public void onPause() {
        super.onPause();
        if (isRemoving() || getActivity().isFinishing()/* or check for change rotation event*/) {
            if (null != snm && snm.isAdded()) getFragmentManager().beginTransaction().remove(snm).commit();
            snm = null;
        }
    }
...

ultraon avatar Mar 17 '15 15:03 ultraon