Difficulty understanding "extends Application"

2

Good evening, I'm having trouble understanding an example

I will try to explain without posting the entire source I think it will be easier to understand

I have a class called GLOBAL that extends Application

I have 3 methods inside it

(...)
public Usuario getUsuario() {
    return usuario;
}

public void setUsuario(Usuario usuario) {
    this.usuario = usuario;
}

public boolean isAutenticado(){
    return getUsuario() != null;
}
(...)

I have a template class named

import java.io.Serializable;

public class Usuario implements Serializable {

    private static final long serialVersionUID = 1L;

    private String nome;

    public Usuario() {
        super();
    }

    public Usuario(String nome) {
        super();
        this.nome = nome;
    }

    public String getNome() {
        return nome == null ? "" : nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }
}

Inside my main activety, there is something very sinister

Well, I'll tell you that I did the configuration on Manifest

(...)
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    iniciar();
    app = (MRCDroidAplicacao) getApplication();
}
(...)

That's where zica comes from, in the example of the teacher it works, in this mine I'm posting does not work

Usuario usuario = new Usuario();
usuario.setNome("USUARIO TESTE 1");
app.setUsuario(usuario);
Log.i("TESTE", "Usuario: " + app.getUsuario());
(...)

I do not understand what I'm doing so it's difficult UHEUHEUHE, I'm copying it

    
asked by anonymous 04.06.2014 / 03:47

1 answer

4

The code that causes problem is just the one that posted:

(...)

    public void onCompleted(GraphUser user, Response response) {
        if (user != null) {
            Usuario usuario = new Usuario(user.getFirstName());
            app.setUsuario(usuario);
            Log.i("TESTE", "Usuario: " + app.getUsuario().getNome());
        }
    }
}).executeAsync();

Log.i("teste", "autenticado" + app.isAutenticado());

Looking at this code, it is missing an important section, but you can guess.

This section, which involves onCompleted is running assíncrona (executeAsync), that is, it is running in parallel (possibly on a Thread) with the next line Log.i("teste", "autenticado" + app.isAutenticado()); .

The problem in this case is that the Log.i("teste", "autenticado" + app.isAutenticado()); line is executed before of app.setUsuario(usuario); , precisely because of parallelism. At the moment you run Log , the user is not set to the Application instance, so the false value.

The recommendation would be to migrate all user-dependent code into onCompleted , like this:

(...)

    public void onCompleted(GraphUser user, Response response) {
        if (user != null) {
            Usuario usuario = new Usuario(user.getFirstName());
            app.setUsuario(usuario);
            Log.i("TESTE", "Usuario: " + app.getUsuario().getNome());
            // .. Qualquer código que dependa do objeto setado no Application
            Log.i("teste", "autenticado" + app.isAutenticado());
        }
    }
}).executeAsync();

From what I saw to your doubts, I recommend reading two "articles": scheduling-concurrent-e-threads . They are not very related to the main problem mentioned, but I think it should help to understand all this code.

    
04.06.2014 / 04:35