Reputation: 57
Hello again everyone,
This time I have a problem with Intent & Extras.
here's my first activity
Intent i1 = new Intent(RoomSetting.this, App.class);
i1.putExtra("subposition1", "right");
final int result=1;
startActivityForResult(i1, result);
and in the 2nd activity I try to get "subposition1" value
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mAccVals = new Number3d();
Bundle extras = getIntent().getExtras();
String SubPos = extras.getString("subposition1");
}
My question is, how can I use SubPos on another method?
Sorry for noobish question. Thanks in advance
Upvotes: 1
Views: 220
Reputation: 771
There are 2 ways of doing it easyly:
First way: Use a global variable for the class:
String SubPos;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mAccVals = new Number3d();
Bundle extras = getIntent().getExtras();
SubPos = extras.getString("subposition1"); //note that I've deleted the 'String'
}
2n way: Pass to the method the variable:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mAccVals = new Number3d();
Bundle extras = getIntent().getExtras();
String SubPos = extras.getString("subposition1");
otherMethod(SubPos);
}
public void otherMethod(String s) {
//do stuff.
}
I considered that maybe you want this variable in another class, then you could do it adding this function (having the String SubPos as global in the class):
String SubPos;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mAccVals = new Number3d();
Bundle extras = getIntent().getExtras();
SubPos = extras.getString("subposition1"); //note that I've deleted the 'String'
}
public String getSubPos() {
return SubPos;
}
This way you can call the instanceOfTheClass.getSubPos() in any other class that has the instance.
Upvotes: 1
Reputation: 11050
Just call this on your "other method".
String subposition = getIntent().getStringExtra("subposition1");
Upvotes: 1
Reputation: 18592
Replace
Bundle extras = getIntent().getExtras();
String SubPos = extras.getString("subposition1");
with
String SubPos = getIntent().getStringExtra("subposition1");
Upvotes: 1
Reputation: 132982
declare SubPos before onCreate
as:
public String SubPos=null; // or you can also declare it as static as your need
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mAccVals = new Number3d();
Bundle extras = getIntent().getExtras();
SubPos = extras.getString("subposition1");
}
public void testmethod()
{
String str=SubPos; //get SubPos value
}
Upvotes: 1
Reputation: 40416
String SubPos;
// declare SubPos String as class variable and you can access anywhere in class<<<
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mAccVals = new Number3d();
Bundle extras = getIntent().getExtras();
SubPos = extras.getString("subposition1");
}
public void anotherMethod(){
//here you can access it...
}
Upvotes: 1