getFragmentManager null the second time

1

Good morning, I've been programming on Android for a few weeks and I'm having some problems with using getFragmentManager .

I have read post about the termination of activities and this is what I have made clear:

Stack Over Flow Quote English 1

  

Also onDestroy () is not a destructor. It does not really destroy the object, it's just a method that delivers a certain state.

in addition to this:

Stack Over Flow Quote English 2

  

Consider then that your application will be in the background and then it dies. When you come back Android will remember that you had Fragments, for example A, B and C and the manager will recreate them and then add them.

I call the method 2 times from one of my activities, and the second time (after doing a getActivity.finish() , recreate the activity and return to the previous fragment method), point me to null.

The point of failure is:

        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); // <<--- AQUI!!!
        infoF.actualizaInfoEstadoIA();
        infoF.onResume();
    }
};

E infoF is what gives null

infoF = (InformacionFragment) getFragmentManager().findFragmentById(R.id.posicionamiento_layout);

This returns me null

I've been debugging the app and re-creating activities and fragments is correct, so it should not point to null .

My question is:

With the read, I have intuited that you are trying to point to the old fragment (1 time executed), since finish () does not really erase 100% the Activity (and I assume that fragments either) . How could I solve it?

    
asked by Mario López Batres 03.11.2016 в 10:51
source

2 answers

1

Finally I solved my problem. Apparently, when doing a finish () you do not completely destroy the activity, but in savedInstanceState some information is saved, and therefore when trying to point to the Fragment, it returns a null.

The solution is the following:

  • Just create a new variable type Bundle as a global variable:

    Bundle datosVuardados;

  • To store savedInstanceState information

    SavedData = savedInstanceState;

  • And check that said variable is not null before calling getFragmentManager

    if (savedData! = null) infoF = (InformacionFragment) getFragmentManager (). findFragmentById (R.id.layout_layout);

answered by 11.11.2016 / 10:51
source
0

Calling finish() completely destroys the Activity, but before it is called (if it is implemented) the method onSaveInstanceState () , which is where you can save desired values, as an example save the value of the user:

  @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {

        savedInstanceState.putString("username", user);

        super.onSaveInstanceState(savedInstanceState);
    }

in this case you can also use it to know if there was an instance previously, and you also get the value saved using onSaveInstanceState () , example:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lomasvisto);


        if (savedInstanceState == null) {
            //Obtiene valor guardado en onSavedInstanceState, y necesitado en el Fragment
            String user = getIntent().getExtras().getString("username");

            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            transaction.replace(R.id.content, MyFragmento.newInstance(user));
            transaction.commit();
        }
    }
}
  

onSaveInstanceState () Call to retrieve the status by   instance of an activity before being deleted so that the state   can be restored on onCreate (Bundle) or onRestoreInstanceState   (Bundle) (the Bundle populated by this method will be passed to both).

    
answered by 19.04.2017 в 17:03