Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2644207
  • 博文数量: 416
  • 博客积分: 10220
  • 博客等级: 上将
  • 技术积分: 4193
  • 用 户 组: 普通用户
  • 注册时间: 2006-12-15 09:47
文章分类

全部博文(416)

文章存档

2022年(1)

2021年(1)

2020年(1)

2019年(5)

2018年(7)

2017年(6)

2016年(7)

2015年(11)

2014年(1)

2012年(5)

2011年(7)

2010年(35)

2009年(64)

2008年(48)

2007年(177)

2006年(40)

我的朋友

分类: Java

2012-02-01 10:48:02

参考:http://www.ibm.com/developerworks/cn/opensource/os-cn-android-actvt/
用了两个android自带的样例:PersistentState、FragmentHideShow

用了三种方式:
A、Intent
B、Bundle
C、SharedPreferences

提示:若在不同的activity中调用getPreferences来共享数据,由于getPreferences源代码是:
return getSharedPreferences(getLocalClassName(), mode);
因此在更换成getSharedPreferences

1. class PersistentState中,添加数据及起动FragmentHideShow
 @Override
    protected void onPause() {
        super.onPause();

        SharedPreferences.Editor editor = getSharedPreferences("share", 0).edit();
        editor.putString("text", mSaved.getText().toString());
        editor.putInt("selection-start", mSaved.getSelectionStart());
        editor.putInt("selection-end", mSaved.getSelectionEnd());
        editor.commit();
        
     // 创建一个带“收件人地址”的 email 
        Bundle bundle =new Bundle();// 创建 email 内容
        bundle.putBoolean("boolean_key", true);// 编写内容
        bundle.putString("string_key", "string_value"); 

        Intent intent =new Intent(this.getBaseContext(), FragmentHideShow.class);
        intent.putExtra("key", bundle);// 封装 email 
        intent.putExtra("string_key", "hello world");
        intent.putExtra("boolean_key", true);
        startActivity(intent); 
    }

2. class FragmentHideShow中接收数据
 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        setContentView(R.layout.fragment_hide_show);
        Intent intent = getIntent();
        Bundle bundle = intent.getBundleExtra("key");
        Boolean b = bundle.getBoolean("boolean_key");
        String val = bundle.getString("string_key");
        String val2 = intent.getStringExtra("string_key");
        Boolean b2 = intent.getBooleanExtra("boolean", true);
        
        SharedPreferences prefs = this.getSharedPreferences("share", 0); 
        String restoredText = prefs.getString("text", "march");
        
        setProgressBarIndeterminateVisibility(true); 

        // 明确进度条
        /*requestWindowFeature(Window.FEATURE_PROGRESS); 
        setContentView(R.layout.fragment_hide_show); 
        setProgress(5000);*/ 
        
        // The content view embeds two fragments; now retrieve them and attach
        // their "hide" button.
        FragmentManager fm = getFragmentManager();
        addShowHideListener(R.id.frag1hide, fm.findFragmentById(R.id.fragment1));
        addShowHideListener(R.id.frag2hide, fm.findFragmentById(R.id.fragment2));
    }


阅读(1611) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~