本节依然是Activity间传递数据的实例,只是过程略显复杂。
总共定义了三个Activity。程序启动后首先执行RedirectEnter,点击按钮后跳到RedirectMain中,如果RedirectMain中没有之前已存储好的信息,则马上直接跳到RedirectGetter中,输入信息,跳回RedirectMain。
RedirectEnter.java
1 //程序启动后首先进入此Activity,点击go按钮后跳往RedirectMain
2 public class RedirectEnter extends Activity
3 {
4 @Override
5 protected void onCreate(Bundle savedInstanceState)
6 {
7 super.onCreate(savedInstanceState);
8
9 setContentView(R.layout.redirect_enter);
10
11 Button goButton = (Button)findViewById(R.id.go);
12 goButton.setOnClickListener(mGoListener);
13 }
14
15 private OnClickListener mGoListener = new OnClickListener()
16 {
17 public void onClick(View v)
18 {
19 // 跳转到RedirectMain界面上
20 Intent intent = new Intent(RedirectEnter.this, RedirectMain.class);
21 startActivity(intent);
22 }
23 };
24 }
RedirectMain.java
1 public class RedirectMain extends Activity {
2 static final int INIT_TEXT_REQUEST = 0;
3 static final int NEW_TEXT_REQUEST = 1;
4
5 @Override
6 protected void onCreate(Bundle savedInstanceState) {
7 super.onCreate(savedInstanceState);
8
9 setContentView(R.layout.redirect_main);
10
11 Button clearButton = (Button)findViewById(R.id.clear);
12 clearButton.setOnClickListener(mClearListener);
13 Button newButton = (Button)findViewById(R.id.newView);
14 newButton.setOnClickListener(mNewListener);
15
16 // 如果sharedPreferences中没有初始值,则直接跳入RedirectGetter中
17 if (!loadPrefs()) {
18 Intent intent = new Intent(this, RedirectGetter.class);
19 startActivityForResult(intent, INIT_TEXT_REQUEST);
20 }
21 }
22
23 @Override
24 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
25 if (requestCode == INIT_TEXT_REQUEST) {
26
27 if (resultCode == RESULT_CANCELED) {
28 finish();
29
30 } else {
31 loadPrefs();
32 }
33
34 } else if (requestCode == NEW_TEXT_REQUEST) {
35
36 if (resultCode != RESULT_CANCELED) {
37 loadPrefs();
38 }
39
40 }
41 }
42
43 // 读取SharedPreferences中数据的函数,若其中没有数据,则返回false
44 private final boolean loadPrefs() {
45 SharedPreferences preferences = getSharedPreferences("RedirectData", 0);
46
47 mTextPref = preferences.getString("text", null);
48 if (mTextPref != null) {
49 TextView text = (TextView)findViewById(R.id.text);
50 text.setText(mTextPref);
51 return true;
52 }
53
54 return false;
55 }
56
57 // 擦除SharedPreferences中的数据,结束本Activity
58 private OnClickListener mClearListener = new OnClickListener() {
59 public void onClick(View v) {
60 SharedPreferences preferences = getSharedPreferences("RedirectData", 0);
61 preferences.edit().remove("text").commit();
62 finish();
63 }
64 };
65
66 // 点击new Text 按钮后,跳往RedirectGetter,重新获取输入信息
67 private OnClickListener mNewListener = new OnClickListener() {
68 public void onClick(View v) {
69 Intent intent = new Intent(RedirectMain.this, RedirectGetter.class);
70 startActivityForResult(intent, NEW_TEXT_REQUEST);
71 }
72 };
73
74 private String mTextPref;
75 }
RedirecGetter.java
1 public class RedirectGetter extends Activity
2 {
3 @Override
4 protected void onCreate(Bundle savedInstanceState)
5 {
6 super.onCreate(savedInstanceState);
7
8 setContentView(R.layout.redirect_getter);
9
10 Button applyButton = (Button)findViewById(R.id.apply);
11 applyButton.setOnClickListener(mApplyListener);
12
13 //R.id.text本来是EditText,但把它强转位TextView类型
14 mText = (TextView)findViewById(R.id.text);
15 }
16
17 // 这个函数在本类中没有被任何一处调用,我把它注销掉再运行一遍程序,丝毫不影响功能的实现,不知google的程序员为什么把它放在这里
18 private final boolean loadPrefs()
19 {
20
21 SharedPreferences preferences = getSharedPreferences("RedirectData", 0);
22
23 mTextPref = preferences.getString("text", null);
24 if (mTextPref != null) {
25 mText.setText(mTextPref);
26 return true;
27 }
28
29 return false;
30 }
31
32 // 点击Apply按钮后,将输入的信息存入SharedPreferences中
33 private OnClickListener mApplyListener = new OnClickListener()
34 {
35 public void onClick(View v)
36 {
37 SharedPreferences preferences = getSharedPreferences("RedirectData", 0);
38 SharedPreferences.Editor editor = preferences.edit();
39 editor.putString("text", mText.getText().toString());
40
41 if (editor.commit()) {
42 setResult(RESULT_OK);
43 }
44
45 finish();
46 }
47 };
48
49 private String mTextPref;
50 TextView mText;
51 }