I have a major problem dealing with onCreate() in Activity. Like the thread said, I can only execute some part of codes in onCreate() method of the main Activity once. So I followed the steps in that thread and do the following:
/*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);
}
}
}
And in the MyChildApp class, I called finish()
when work is done there. However, the field wascalled
is always false. I think when the onCreate()
is execute the second time when returning from MyChildApp
, wascalled
should be set to true already. However it isn't. And the code within the if statement in onCreate()
method is executed every time when come back from MyChildApp
.
Does anyone have advices on this? Thanks a lot in advance.