Mario López Batres Mario López Batres - 29 days ago 10
Android Question

getFragmentManager() returns null on second call

I've been for learning how to code in Android, but I am having some problems.

Right now, I do not really know how to finish a

Fragment
. I mean, when I use the method
getActivity().finish()
, my
Activity
should
finish()
. I've been reading about it, and it is not correct at all. I came across this post:


Also, onDestroy() isn't a destructor. It doesn't actually destroy the
object. It's just a method that's called based on a certain state. So
your instance is still alive and very well* after the superclass's
onDestroy() runs and returns.Android keeps processes around in case
the user wants to restart the app, this makes the startup phase
faster. The process will not be doing anything and if memory needs to
be reclaimed, the process will be killed


And this one:


Consider that you then background your application, and then it gets
killed. When you come back, Android will remember that you used to
have Fragments A, B and C in the fragment manager and so it recreates
them for you and then adds them. However, the ones that are added to
the fragment manager now are NOT the ones you have in your fragments
list in your Activity.



In my case, I have two activities and two fragments. The first time that I used this process it worked perfectly.


  • I call to my second
    Activity
    from the first one using an
    Intent

  • I call to the first
    Fragment

  • I call from the first
    Fragment
    to the second one



The problem is that when I press the button "reset" that it finish my
Activity
from the second
Fragment
(so it goes to the first
Activity
). After this, I repeat the entire process and it actually works but when I try to implement the next line to keep updating data:

private BroadcastReceiver estadoIAReceiver = new BroadcastReceiver() {

//Cuando cambia el estado de indoor atlas actualizo la interfaz con sus propiedades
@Override
public void onReceive(Context context, Intent intent) {
Log.i("VFragment", "Cambio estado IA");

//Ejecutar metodo de un fragment desde una actividad
infoF = (InformacionFragment) getFragmentManager().findFragmentById(R.id.posicionamiento_layout);
infoF.actualizaInfoEstadoIA();
infoF.onResume();
}
};


It fails because
getFragmentManager().findFragmentById(R.id.posicionamiento_layout);

returns
null
. So, basically my question is; is
infoF
trying to access to the same
Fragment
from the first time but it does not exist so it returns
null
?

Answer

Finally, some days ago I could find my error.

The solution is simple:

  • Just create a new Bundle variable as a global variable

Bundle datosGuardados;

  • to store the data of savedInstanceState

datosGuardados = savedInstanceState;

  • and finally just check if it is null before than using any getFragmentManager

if (datosGuardados != null) infoF = (InformacionFragment) getFragmentManager().findFragmentById(R.id.posicionamiento_layout);