1 /* 2 * Copyright (C) 2007 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.example.android.apis.app; 18 19 import com.example.android.apis.R; 20 21 import android.app.Activity; 22 import android.content.SharedPreferences; 23 import android.os.Bundle; 24 import android.view.View; 25 import android.view.View.OnClickListener; 26 import android.widget.Button; 27 import android.widget.TextView; 28 29 /** 30 * Sub-activity that is executed by the redirection example when input is needed 31 * from the user. 32 */ 33 public class RedirectGetter extends Activity 34 { 35 @Override 36 protected void onCreate(Bundle savedInstanceState) 37 { 38 super.onCreate(savedInstanceState); 39 40 setContentView(R.layout.redirect_getter); 41 42 // Watch for button clicks. 43 Button applyButton = (Button)findViewById(R.id.apply); 44 applyButton.setOnClickListener(mApplyListener); 45 46 // The text being set. 47 mText = (TextView)findViewById(R.id.text); 48 } 49 50 private final boolean loadPrefs() 51 { 52 // Retrieve the current redirect values. 53 // NOTE: because this preference is shared between multiple 54 // activities, you must be careful about when you read or write 55 // it in order to keep from stepping on yourself. 56 SharedPreferences preferences = getSharedPreferences("RedirectData", 0); 57 58 mTextPref = preferences.getString("text", null); 59 if (mTextPref != null) { 60 mText.setText(mTextPref); 61 return true; 62 } 63 64 return false; 65 } 66 67 private OnClickListener mApplyListener = new OnClickListener() 68 { 69 public void onClick(View v) 70 { 71 SharedPreferences preferences = getSharedPreferences("RedirectData", 0); 72 SharedPreferences.Editor editor = preferences.edit(); 73 editor.putString("text", mText.getText().toString()); 74 75 if (editor.commit()) { 76 setResult(RESULT_OK); 77 } 78 79 finish(); 80 } 81 }; 82 83 private String mTextPref; 84 TextView mText; 85 } 86 87