アクティビティで onCreate() を扱うのに大きな問題があります。スレッドが言ったように、メイン アクティビティの onCreate() メソッドでコードの一部を 1 回しか実行できません。そのため、そのスレッドの手順に従い、次のことを行います。
/*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()
から戻ったときに が 2 回目に実行されるときはMyChildApp
、wascalled
すでに true に設定する必要があると思います。しかし、そうではありません。そして、メソッド内の if 文内のコードは、onCreate()
から戻るたびに実行されMyChildApp
ます。
これについて誰かアドバイスはありますか?よろしくお願いします。