RNK
RNK

Reputation: 5792

Access on create variables through out the android activity class

I am getting transaction_amount variable from getIntent() in OnCreate method.

String transaction_amount = getIntent().getStringExtra("transaction_amount");

I want to access this variable outside of the OnCreate method. Something like,

public class PayActivity extends ActionBarActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        String transaction_amount = getIntent().getStringExtra("transaction_amount");
    }

    protected void my_function(){
        // I want transaction_amount here.....
    }
}

Upvotes: 0

Views: 97

Answers (4)

varun
varun

Reputation: 1523

Declare a global variable and intialize it to the transactionamount in onCreate. Then use that global variable anywhere.

  public class PayActivity extends ActionBarActivity {
   String s; 

   @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    s = getIntent().getStringExtra("transaction_amount"); 
  }

  void anyFunction() {
   // use the string s here now

   }

  }

Upvotes: 0

amar
amar

Reputation: 147

maintain transaction amount as a global variable ,if you require that variable in whole class or if you want acces that variable in method, send that variable as a parameter to that function and called that function from oncreate.

Upvotes: 0

Shooky
Shooky

Reputation: 1299

create the transaction_amount variable as a class variable, then initialize it in the onCreate

public class PayActivity extends ActionBarActivity {
private String transaction_amount;
@Override
protected void onCreate(Bundle savedInstanceState) {
      transaction_amount = getIntent().getStringExtra("transaction_amount");
}

protected void my_function(){
    // I want transaction_amount here.....
}

}

Upvotes: 0

Rogue
Rogue

Reputation: 779

You can use a global variable :

public class PayActivity extends ActionBarActivity {
    String value;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        value = getIntent().getStringExtra("transaction_amount");
    }
    public void yourFunction() {
        // You can acces value here
    }

// your activity here....
}

Upvotes: 1

Related Questions