Display value from another screen

Asked

Viewed 77 times

0

Hello I’m working with the following code:

   @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_conversor);
        String t = Double.toString(getIntent().getExtras().getDouble("ask"));

    }

The string t is a datum taken from another Activity through the code:

Intent intent = new Intent(Tela1.this, conversor.class);
                            intent.putExtra("ask",ask);
                            startActivity(intent);

The problem is the following I can only display the value t if it is inside the "protected void onCreate" it only works inside, as serie to get the value outside?

1 answer

1

Declare the String t out of your onCreate(). Behold:

private String t;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_conversor);
    t = Double.toString(getIntent().getExtras().getDouble("ask"));
}

There you can use in other parts of your code.

If you want to use this variable in another Activity, you can declare as public static:

public static String t;

So, to access it from another Activity just do it this way:

String outroT = OutraActivity.t;

Browser other questions tagged

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