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
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).– Wakim
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
– vandrebr1
Actually, you need to log in that way:
Log.i("TESTE", "Usuario: " + app.getUsuario.getNome());
or overwrite thetoString
classUsuario
to return the name. Now depending on the order that is calling theisAutenticado
, he should returntrue
, because the user state in the instance ofApplication
.– Wakim
Uhmm, perfect, it worked using app.getUsuario.getNome()); but how would I do for ( app.setUsuario(user) and then isAutenticado() return me true
– vandrebr1
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 theisAutenticado
before setting theusuario
in it.– Wakim
could, but I did not understand the logic of business: app.setUsuario(user) example; (as he knows I am passing the user name)
– vandrebr1
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());
– vandrebr1
Ah, now it makes sense not to work... I will create an answer based on that, I think it better to explain by comments.
– Wakim
:) many thanks,
– vandrebr1