我在处理 Activity 中的 onCreate() 时遇到了一个主要问题。就像线程说的,我只能在主Activity的onCreate()方法中执行部分代码一次。所以我按照该线程中的步骤并执行以下操作:
/*I've updated the code to SharePreferences version*/
public class MyApp extends Activity {
private static RMEFaceAppActivity instance;
private boolean wascalled = false;
private SharedPreferences sharedPreferences;
private SharedPreferences.Editor editor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//initiate some buttons here
sharedPreferences = this.getSharedPreferences("test",0);
editor = sharedPreferences.edit();
if(sharedPreferences.getString("wascalled", "false").equalsIgnoreCase("false"))
{
//work can only be done once
editor.putString("wascalled", "true");
editor.commit();
}
//set buttons to listener methods
}
void onClick(View arg0)
{
if(arg0.getId() == R.id.button1)
{
Intent intent = new Intent(this, MyChildApp.class);
startActivity(intent);
}
}
}
在 MyChildApp 类中,我finish()
在那里完成工作时调用。但是,该字段wascalled
始终为 false。我认为当onCreate()
从返回时第二次执行时MyChildApp
,wascalled
应该已经设置为true。然而事实并非如此。并且onCreate()
每次从MyChildApp
.
有人对此有建议吗?提前非常感谢。