RxPermissions icon indicating copy to clipboard operation
RxPermissions copied to clipboard

Important: Cannot get current activity/fragment to reference in the callback

Open akidee opened this issue 8 years ago • 2 comments

The callback may cross the lifetimes of different activity instances. Changing orientation while the permission dialog is showing up lets the callback code break because it references members of the old activity instance. Solution: Pass the current activity to the callback (as part of an object or as second argument) to get a reference to a not yet existing activity instance. This is not an edge case but should be the rule and is more elegant than my current patch:

public class MyActivity extends Activity {

    static MyActivity currentInstance;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // To reference in request callback
        currentInstance = this;

        // ...
    }
}

Example: https://github.com/VictorAlbertos/RxActivityResult where a

safety instance of the current Activity/Fragment

can be accessed from the callback result.

akidee avatar Jun 18 '17 20:06 akidee

Hello. I think this is not realy cool, what you talkink about. Just IMHO. Pass an activity to somewhere, where there reference may be saved in static context? No-o, not for me. I don't see the problem in recreating activity while orientation change. Just subscribe RxPermition in onStart(), unsubscribe chain in onStop(), and you're good to go, aren't you ? Permission dialog will be showed again. No problems

And thigs, that you do in your example, not so obvious for me ...

mrArtCore avatar Jun 19 '17 19:06 mrArtCore

totally agree with @mrArtCore

epool avatar Jun 23 '17 21:06 epool