startActivity(intent);
[RedirectMain.java]
static final int INIT_TEXT_REQUEST = 0;
static final int NEW_TEXT_REQUEST = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.redirect_main);
if (!loadPrefs()) {
Intent intent = new Intent(this, RedirectGetter.class);
startActivityForResult(intent, INIT_TEXT_REQUEST);
}
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
if (requestCode == INIT_TEXT_REQUEST) {
if (resultCode == RESULT_CANCELED) {
finish();
} else {
loadPrefs();
}
} else if (requestCode == NEW_TEXT_REQUEST) {
if (resultCode != RESULT_CANCELED) {
loadPrefs();
}
}
}
SharedPreferences preferences = getSharedPreferences("RedirectData", 0);
if (mTextPref != null) {
TextView text = (TextView)findViewById(R.id.text);
text.setText(mTextPref);
return true;
}
}
public void onClick(View v) {
SharedPreferences preferences = getSharedPreferences("RedirectData", 0);
preferences.edit().remove("text").commit();
finish();
}
};
public void onClick(View v) {
Intent intent = new Intent(RedirectMain.this, RedirectGetter.class);
startActivityForResult(intent, NEW_TEXT_REQUEST);
}
};
}
[RedirectGetter.java]
private final boolean loadPrefs()
{
SharedPreferences preferences = getSharedPreferences("RedirectData", 0);
mTextPref = preferences.getString("text", null);
if (mTextPref != null) {
mText.setText(mTextPref);
return true;
}
return false;
}
private OnClickListener mApplyListener = new OnClickListener()
{
public void onClick(View v)
{
SharedPreferences preferences = getSharedPreferences("RedirectData", 0);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("text", mText.getText().toString());
if (editor.commit()) {
setResult(RESULT_OK);
}
finish();
}
};
// "RedirectData"는 String name, "text"는 String key
** RedirectEnter.java에서 RedirectMain.java를 호출하고 RedirectMain은 prefrences의 내용 유무에 따라서 sub activity를 실행하여 그 데이터를 저장하고 출력한다. **
'old > API_Demo' 카테고리의 다른 글
APP_Activity_SetWallpaper (0) | 2010.03.16 |
---|---|
APP_Activity_ReorderActivities (0) | 2010.03.16 |
APP_Activity_ReceiveResult (0) | 2010.03.16 |
APP_Activity_Persistent State (0) | 2010.03.15 |
APP_Activity_Hello World (0) | 2010.03.15 |