0

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?

Community
  • 1
  • 1
  • 1
    I might not understand your question, but if the fragment is not in FragmentManager (is null), then you should add a new one to it because it was destroyed,and removed from Fragmentmanager – Vladyslav Matviienko Nov 03 '16 at 12:33
  • I mean, the code works the first time (so during the first time that i execute it, i do not get "null"). So i do not really know why do i get it @Bryan – Mario López Batres Nov 03 '16 at 12:45
  • I think you meant @VladMatvienko. Anyway, show more code; there is nothing in the code you provided that shows why `getFragmentMangager()` would return `null`. Also this line: "I call from the first `Fragment` to the second one" sounds suspicious. You should not be calling from one `Fragment` to another, the `Activity` should handle transactions between fragments. – Bryan Nov 03 '16 at 12:50
  • Thank you for your help. – Mario López Batres Nov 13 '16 at 19:11

1 Answers1

0

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