您好,登錄后才能下訂單哦!
數(shù)據(jù)持久化:
由于更高優(yōu)先級Activity的跳轉(zhuǎn),沒有對當前頁面數(shù)據(jù)的及時保存,導致原本已經(jīng)輸入的數(shù)據(jù)丟失。
通過打印日志,不然發(fā)現(xiàn):Activity的跳轉(zhuǎn)過程中,必須執(zhí)行onstop方法,而Activity的重現(xiàn),必須執(zhí)行OnStart方法,所有數(shù)據(jù)持久化,就是在onstop方法中,對數(shù)據(jù)進行進行保存
在OnStart方法中,對數(shù)據(jù)進行讀取,并顯示在原來的位置上
接下來就是相應(yīng)的步驟:
1、在onstop方法中:
@Override
protected void onStop()
{
Log.e("MainActivity", "onStop");
super.onStop();
FileOutputStream fos=null;
try
{
fos=new FileOutputStream(PATH);
fos.write(et_account.getText().toString().getBytes());
}
catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(fos!=null){
try
{
fos.close();
}
catch (IOException e)
{
Log.e("MainActivity", "關(guān)閉流失敗");
}
}
}
}
2、在onstart方法中:
@Override
protected void onStart()
{
Log.e("MainActivity", "onStart");
super.onStart();
FileInputStream fis=null;
StringBuffer buffer=new StringBuffer();
try
{
fis=new FileInputStream(PATH);
int len;
byte b[]=new byte[1024];
while(-1!=(len=fis.read(b))){
buffer.append(new String(b, 0,len));
}
et_account.setText(buffer.toString().trim());
}
catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(fis!=null){
try
{
fis.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
3、最后別忘了加權(quán)限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
如果在genymotion模擬器中,雖然不加權(quán)限也可以,但是在真機以及官方模擬器上面,不加權(quán)限就不能用了
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。