سؤال

I have implemented a Fragment and override its lifecycle callbackas as following:

@Override
    public void onActivityCreated(final Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        dactivity = getActivity();
        dactivity.bindService(new Intent(dactivity,EventosDatabaseService.class), this, 0);
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        dactivity.unbindService(this);
    }

It is documented that onActivityCreated call must heppen prior to onDestroy, then how come i get NullPointerException thrown because dactivity is null on onDestroy? what should i do to avoid it while making sure the binding wont produce a leak?

BTW, the RetainInstance of this fragment is false if that metters

هل كانت مفيدة؟

المحلول

Try bind your service in Fragment.onResume with registering broadcast listeners if available, and use Fragment.onPause to unbind service and unregister listeners

نصائح أخرى

I wish the image i posted could offer better understanding to figure out how fragments work. enter image description here

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top