Send data from one method to the other in the same Activity

Asked

Viewed 124 times

2

I know that to send data from one activity to another is the following:

String value = filename;
Intent intent = new Intent(getApplicationContext(), ReceberNome.class);
intent.putExtra("nameFile", value);
startActivity(intent);

And to receive:

String value = null;
Bundle bundle = getIntent().getExtras();
if (bundle != null){
value = bundle.getString("namefile");

It turns out that I nay I want to send data of a activity to another, and yes take dice deposited in a method and send to another the Activity method.

private void PegarNome(){
//Aqui eu consigo um valor onde não posso chamar no outro método, que é o filename

String value = filename;
Intent intent = new Intent(getApplicationContext(), ReceberNome.class);
intent.putExtra("nameFile", value);
startActivity(intent);
}

private void ReceberNome(){
//Aqui quero receber o filename que é obtido somente no método PegarNome()
}

To send I already tried:

String value = filename;
Intent intent = getIntent();
intent.putExtra("nameFile", value);
startActivity(intent);

But I didn’t get the value, thank you very much!

2 answers

2


You just pass to a private variable in the class.

private String value;
private void PegarNome(){
//Aqui eu consigo um valor onde não posso chamar no outro método, que é o filename

value = filename;
Intent intent = new Intent(getApplicationContext(), ReceberNome.class);
intent.putExtra("nameFile", value);
startActivity(intent);
}

private void ReceberNome(){
//Aqui quero receber o filename que é obtido somente no método PegarNome()

 System.out.println("O nome do arquivo é: "+value);
}
  • Thanks a lot bro, but what about Intent? In this code he keeps sending the data to another activity. How do you do with this part: Intent Intent = new Intent (getApplicationContext(), Payname.class);?

  • 1

    If you are in the same Activity, you do not need to start an Intent, just call the method and retrieve the variable value.

  • 1

    Exactly. Thank you very much ;)

1

You can pass as parameter in the method too, example:

meuMetodo("Leonardo", "28 anos", "masculino");

And to receive:

public void meuMetodo(String nome, String idade, String sexo){
     //Aqui usa as variáveis que recebeu
}

Hugs.

Browser other questions tagged

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