Difficulty to understand "extends Application"

Asked

Viewed 73 times

2

Good night, I’m having trouble understanding an example

I’ll try to explain without posting the entire font I think will be easier to understand

I have a class called GLOBAL that extends Application

Inside it I have 3 ways

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

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

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

There I have a model class called

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’s something very sinister

Well I’ll tell you that I did the configuration in the Manifest

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

Here comes the Zica, in the example of the teacher works, in the one 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 don’t understand what I’m doing so ta dificil UHEUHEUHE, I’m copying from him

  • 1

    Could you tell us what the problem is? It is normal to have a class that extends Application. An instance of this class is available as the Application Context (it is only destroyed when the user leaves the application), so it is normal to save status to it (saved in moments when the system has little memory).

  • Hello friend, good night, What happens is: in the log: Log. i("TESTE", "Usuario: " + app.getUsuario()); the output is: 06-03 22:09:24.569: I/TESTE(2308): Usuario: br.com.mrcsistemas.mrcdroid.modelo.Usuario@b 31c74f8 and I was waiting for the logged in username as I have the public function Boolean isAutenticado(){ Return getUsuario() != null; } which always returns false to me

  • 1

    Actually, you need to log in that way: Log.i("TESTE", "Usuario: " + app.getUsuario.getNome()); or overwrite the toString class Usuario to return the name. Now depending on the order that is calling the isAutenticado, he should return true, because the user state in the instance of Application.

  • Uhmm, perfect, it worked using app.getUsuario.getNome()); but how would I do for ( app.setUsuario(user) and then isAutenticado() return me true

  • You say the isAutenticado? Could you show a more complete use case? Showing some order, because it seems to me that it is a competition problem, is accessing the isAutenticado before setting the usuario in it.

  • could, but I did not understand the logic of business: app.setUsuario(user) example; (as he knows I am passing the user name)

  • ublic void onCompleted(Graphuser user, Response Response) { if (user != null) { User user = new User(user.getFirstName(); app.setUsuario(user); Log. i("TEST", "User: " + app.getUsuario().getNome(); } } }). executeAsync(); Log. i("test", "authenticated" + app.isAutenticado());

  • Ah, now it makes sense not to work... I will create an answer based on that, I think it better to explain by comments.

  • :) many thanks,

Show 4 more comments

1 answer

4


The code that causes trouble is precisely 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, we’re missing an important piece, but we can guess.

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

The problem in this case is that the line Log.i("teste", "autenticado" + app.isAutenticado()); is previously executed of app.setUsuario(usuario);, precisely by the parallelism. Just at the moment that the Log, the user is not set in the instance of Application, therefore the value false.

The recommendation would be to migrate any user-dependent code into the onCompleted, thus:

(...)

    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 could see of your doubts, I recommend reading two "articles": orientation-to-basic objects and concurrent programming-e-threads. They are not very related to the main problem cited, but I think it should help to understand all this code.

  • Really, and me killing myself, I understood the parallelism yes. And thanks for the articles saying, I don’t have a good knowledge in object orientation, I’ll sort that by way Ctrl+c, I’ll read the 2 yes, IT WORKED, thank you very much

Browser other questions tagged

You are not signed in. Login or sign up in order to post.