gpt4 book ai didi

android - "Activity"类中成员变量的持久化

转载 作者:太空狗 更新时间:2023-10-29 14:28:20 24 4
gpt4 key购买 nike

为什么成员变量“m_lat”和“m_lng”不持久化在“onCreate”和“onClick”调用之间,但 Intent从 getIntent() 返回的是什么?

(我认为 .xml 文件是无关紧要的)

代码如下:

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

}

}

谢谢!

最佳答案

请查看 Android 开发者文档。

public int getIntExtra (String name, int defaultValue)

Since: API Level 1 Retrieve extended data from the intent.

Parameters

name: The name of the desired item.

defaultValue: the value to be returned if no value of the desired type is stored with the given name.

Returns

the value of an item that previously added with putExtra() or the default value if none was found.

http://developer.android.com/reference/android/content/Intent.html#getIntExtra(java.lang.String, int)

关于android - "Activity"类中成员变量的持久化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9611739/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com