android2.3 api demo 学习系列(11)--App/Activity/Redirection

APIDEMO里面的redirection示例本身并没有新技术,里面用到的知识点在前面的几个文章中都已涉及到:

 

  • SharedPreferences
  • startActivityForResult
redirection demo主要展示的是根据不同的条件跳转到特定的activity
里面使用到的几个关键点
1、SharedPreferences
//SharedPreferences 的保存
SharedPreferences sharedPreferences = getSharedPreferences("RedirectData", 0);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("text", inpuTextView.getText().toString());
if (editor.commit()) {
    setResult(RESULT_OK);
}
//SharedPreferences 的读取
SharedPreferences preferences = getSharedPreferences("RedirectData", 0);
String mTextPref = preferences.getString("text", null);
 2、打开activity获取结果
//打开方法
startActivityForResult(new Intent(RedirectMain.this,RedirectInput.class), REQUEST_CODE);
//获取结果方法
@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if (REQUEST_CODE == requestCode && resultCode == RESULT_OK) {
			SharedPreferences preferences = getSharedPreferences("RedirectData", 0);
		    String mTextPref = preferences.getString("text", null);
			textView.setText("用户输入:" + mTextPref);
		}
	}
//被打开的activity返回结果
SharedPreferences sharedPreferences = getSharedPreferences("RedirectData", 0);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("text", inpuTextView.getText().toString());
if (editor.commit()) {
	setResult(RESULT_OK);
}
finish();
 效果图:

android2.3 api demo 学习系列(11)--App/Activity/Redirection
 

你可能感兴趣的:(android,api,demo)