0

I'm currently creating an app using a bluetooth connection. The connection is handled inside an simple object (HandleConnection). When the connection is made, my app goes into "remote" mode, which means I'm replacing my main fragment with another one (RemoteFragment) which use the "HandleConnection" object.

Until the everything is going well, since at the end of my "onCreate" (inside my activity) I set the RemoteFragment's handleConnection attribute:

    @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_layout);


    FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
    if (savedInstanceState == null) {
        retainedFragment = new RetainedFragment();
        getFragmentManager().beginTransaction().add(retainedFragment, RetainedFragment.class.toString()).commit();
        handleConnection = new HandleConnection(this, handler);
        retainedFragment.setHandleConnection(handleConnection);

    } else {

        remoteFragment = (RemoteFragment) getFragmentManager().findFragmentByTag(RemoteFragment.class.toString());
        retainedFragment = (RetainedFragment) getFragmentManager().findFragmentByTag(RetainedFragment.class.toString());

        if (remoteFragment == null)
            remoteFragment = new RemoteFragment();

        handleConnection = retainedFragment.getHandleConnection();
        remoteFragment.setHandleConnection(handleConnection);
    }

    fragmentTransaction.commit();

}

Everything works fine except when I turn into landscape mode. Then (after playing in debug mode), it seems my RemoteFragment is recreated after my setter is called, which obviously mean my handleConnection attribute is null. Why is it doing that ? I would understand if I didn't reuse the previous fragment (then I would have two different fragments on top of each other) but in this case it makes non sense. I made a workaround by calling a callback function inside the onCreateView to get the HandleConnection object, but why would the attribute be null when I called a setter right before ?

Thanks !

lesurp
  • 343
  • 4
  • 19

3 Answers3

0

A Fragment's life cycle is directly affected by the host activity's life cycle. when the activity is destroyed, so are all fragments. During Layout changes, an activity is destroyed and recreated.

In this case, after rotation, your else condition in the onCreate() method will execute and hence the behavior that you are getting. Have a look at the documentation for thorough understanding.

http://developer.android.com/guide/components/fragments.html

Sabeeh
  • 1,123
  • 9
  • 11
  • Yes, but since I'm calling ````remoteFragment.setHandleConnection(handleConnection);```` the remoteFragment shouldn't have a null pointer when entering onCreateView ? – lesurp Mar 15 '16 at 19:45
  • In your else condition when you are calling 'handleConnection = retainedFragment.getHandleConnection();' is this handle Connection null? I guess this is null. You should have a nullability check here before setting it to 'remoteFragment'. – Sabeeh Mar 15 '16 at 20:05
  • No, the handleConnection is not null. I have been using the RetainedFragment pattern mentioned in the android doc here: [link](http://developer.android.com/guide/topics/resources/runtime-changes.html) – lesurp Mar 15 '16 at 20:16
  • Try this `else { remoteFragment = (RemoteFragment)getFragmentManager(). findFragmentByTag(RemoteFragment.class.toString()); retainedFragment = (RetainedFragment)getFragmentManager(). findFragmentByTag(RetainedFragment.class.toString()); if (remoteFragment == null) remoteFragment = new RemoteFragment(); handleConnection = retainedFragment.getHandleConnection(); remoteFragment.setHandleConnection(handleConnection); getFragmentManager().beginTransaction() .add(remoteFragment,RemoteFragment.class.toString()).commit(); }` – Sabeeh Mar 16 '16 at 16:48
0

You need to add to your activity registration in AndroidManifes.xml something like that: <activity android:configChanges="orientation|screenSize"/>

Read this article

0

So apparently my problem was that I wasn't using the right instance of my RemoteFragment, which means my current reference (in my activity) had the correct non-null value for my object, but the fragment displayed wasn't this one (it was the old one which was recreated and thus had a null value for the handleConnection).

lesurp
  • 343
  • 4
  • 19