Persistence of member variables in the class "Activity"
Why are the member variables "m_lat" and "m_lng" not persisting between calls to onCreate and onClick, but the intent is returned from getIntent ()?
(I think the .xml file is irrelevant)
The code follows:
public class MyActivity extends Activity implements OnClickListener {
int m_lat, m_lng;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent i = getIntent();
m_lat = i.getIntExtra("LAT", -1);
m_lng = i.getIntExtra("LNG", -2);
// here, m_lat m_lng have good values
}
@Override
public void onClick(View v) {
Intent oldInt = getIntent();
int new_lat = oldInt.getIntExtra("LAT", 0);
int new_lng = oldInt.getIntExtra("LNG", 0);
// here, m_lat and m_lng are 0, but
// new_lat and new_lng are now valid
}
}
Tks!
+3
source to share
1 answer
Pls check android developer docs.
public int getIntExtra (string name, int defaultValue)
C: API Level 1 Retrieve extended data from intent.
Parameters
name: the name of the desired element.
defaultValue: the value that should be returned if a value of the desired type is not stored with the given name.
Returns
the element value that was previously added with putExtra (), or the default if none was found.
+1
source to share