Mario López Batres Mario López Batres - 1 year ago 95
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

. I mean, when I use the method
, my
. 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
    from the first one using an

  • I call to the first

  • I call from the first
    to the second one

The problem is that when I press the button "reset" that it finish my
from the second
(so it goes to the first
). 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
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(;

It fails because

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

Answer Source

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(;

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download