onSaveInstanceState()方法会携带一个 Bundle 类型的参数,Bundle 提供了一系列的方法用于保存数据,比如可以使用 putString()方法保存字符串,使用 putInt()方法保存整型数据,以此类推。每个保存方法需要传入两个参数,第一个参数是键,用于后面从 Bundle中取值,第二个参数是真正要保存的内容。
在 Activity 中添加如下代码就可以将临时数据进行保存:@Overrideprotected void onSaveInstanceState(Bundle outState) {super.onSaveInstanceState(outState);String tempData = "Something you just typed";outState.putString("data_key", tempData);}
取数据
if (savedInstanceState != null) {String tempData = savedInstanceState.getString("data_key");Log.d(TAG, tempData);}
Intent 还可以结合Bundle 一起用于传递数据的,首先可以把需要传递的数据都保存在 Bundle 对象中,然后再将 Bundle 对象存放在 Intent 里。到了目标活动之后先从 Intent 中取出 Bundle,再从 Bundle中一一取出数据。
转载于:https://www.cnblogs.com/It-Hugo0527/p/5816907.html