gpt4 book ai didi

java - 是否可以在 onCreate() 函数中创建不一致的数据?

转载 作者:行者123 更新时间:2023-11-29 21:36:38 25 4
gpt4 key购买 nike

我有一个关于 onCreate() 函数中数据一致性的小问题。为了更好地理解我的示例:

public class Test extends Activity {

public String isThisConsistent1;
public int isThisConsistent2;

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.Test, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.Item_1:
getSomething();
return true;
default:
return super.onOptionsItemSelected(item);
}
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.Test);

/*Very, very long taking code like a huge loop */

isThisConsistent1 = ...;
isThisConsistent2 = ...;
}

private void getSomething(){
String test1 = isThisConsistent1;
int test2 = isThisConsistent2;
}
}

所以我的问题是:是否可以在 onCreate() 函数完成之前单击 ActionBar 上的 Item?因为那时我会在 onCreate() 函数中设置数据之前调用函数 getSomething()

最佳答案

正如您在 lifecycle documentation 中看到的那样, onCreate 方法在 View 可见之前被调用。这意味着,答案是否定的,您不能在 onCreate 完成之前单击按钮。

关于java - 是否可以在 onCreate() 函数中创建不一致的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18298215/

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