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;

    protected void onCreate(Bundle savedInstanceState) {

        Intent i = getIntent();
        m_lat = i.getIntExtra("LAT", -1);
        m_lng = i.getIntExtra("LNG", -2);

                // here, m_lat m_lng have good values


    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






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.


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.


the element value that was previously added with putExtra (), or the default if none was found. (java.lang.String, int)



All Articles