Display value from another screen

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 data that I get 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 serial to get the value out?

    
asked by anonymous 09.01.2017 / 14:56

1 answer

1

Declare String t out of your onCreate() . See:

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 it in other parts of your code.

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

public static String t;

So, to access it from another activity you just do it like this:

String outroT = OutraActivity.t;
    
09.01.2017 / 15:04