페이지

2011년 5월 23일 월요일

임시 저장 - SharedPreferences

SharedPreferences 는 임시변수나 현재 UI 정보같은 작은 크기의 정보를 저장하기 적합하다.

조금 더 큰 데이터는 직접 파일에 쓰거나 SQL 을 사용하여 DB 에 저장하도록 하여야 한다.

    @Override
    protected void onPause() {
        super.onPause();
        SharedPreferences.Editor editor = getPreferences(0).edit();
        editor.putString("text", mSaved.getText().toString());
        editor.putInt("selection-start", mSaved.getSelectionStart());
        editor.putInt("selection-end", mSaved.getSelectionEnd());
        editor.commit();
    }

    @Override
    protected void onResume() {
        super.onResume();
        SharedPreferences prefs = getPreferences(0);
        String restoredText = prefs.getString("text", null);
        if (restoredText != null) {
            mSaved.setText(restoredText, TextView.BufferType.EDITABLE);
            int selectionStart = prefs.getInt("selection-start", -1);
            int selectionEnd = prefs.getInt("selection-end", -1);
            if (selectionStart != -1 && selectionEnd != -1) {
                mSaved.setSelection(selectionStart, selectionEnd);
            }
        }
    }

액티비티 호출하기 - intent

            Intent intent = new Intent();
            intent.setClass(Forwarding.this-호출하는 액티비티, ForwardTarget.class-호출할 액티비티);
            startActivity(intent);
            finish();

액티비티를 다이얼로그로 띄우기 - DialogActivity

Activity 를 Dialog 로 띄우는 간단한 예제.

CustomDialog 보다 구현하기 편하고 심플하다.

public class DialogActivity extends Activity {

    @Override
 protected void onCreate(Bundle savedInstanceState) {
        // Be sure to call the super class.
        super.onCreate(savedInstanceState);
       
        requestWindowFeature(Window.FEATURE_LEFT_ICON);
        setContentView(R.layout.dialog_activity);
        getWindow().setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,
                android.R.drawable.ic_dialog_alert);
    }
}


AndroidManifest.xml 파일에서 테마를 지정해 준다.

        <activity android:name=".app.DialogActivity"
                android:label="@string/activity_dialog"
                android:theme="@android:style/Theme.Dialog">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.SAMPLE_CODE" />
            </intent-filter>
        </activity>