如何保存数据永久所以后来我可以访问它的Andr​​oid? [英] How do I save data permanently so I can access it later in Android?

查看:131
本文介绍了如何保存数据永久所以后来我可以访问它的Andr​​oid?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我preSS后退按钮我的应用程序不会调用的onSaveInstanceState(),我无法保存数据。我试图做一个调度应用程序,我需要保存已设置即使我preSS后退按钮时间表。我想从源文件到字符串数组添加新的数据动态编辑一个ListView。我遇到的问题是,该文件schedules.txt不是储蓄。每当程序会打开一个新的活动的文件现在空白。

这是code我到目前为止有:

 公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);    的setContentView(R.layout.activity_schedules);
    ListView控件列表视图=(ListView控件)findViewById(R.id.scheduleListView);
    适配器=新ArrayAdapter<串GT;(这一点,android.R.layout.simple_list_item_1,schedulesList);
    listview.setAdapter(适配器);
    Log.v(MyApp的,与此计划相关的文件currentlist是:+的fileList());    尝试{        FileOutputStream中FOUT = openFileOutput(schedules.txt,MODE_PRIVATE);
        Log.v(MyApp的,跑的FileOutputStream);    }赶上(FileNotFoundException异常E1){
        // TODO自动生成catch块
        e1.printStackTrace();
    }尝试{
       Log.v(MyApp的,先变老的时间表称之为);
       getOldSchedules();
}赶上(IOException异常五){
    // TODO自动生成catch块
    e.printStackTrace();
}
公共无效EDITFILE(字符串格式)抛出IOException    Log.v(MyApp的,EDITFILE跑了);
    FileOutputStream中FOUT = openFileOutput(schedules.txt,MODE_PRIVATE);
    OutputStreamWriter作家=新OutputStreamWriter(FOUT);    writer.write(你好亚历克斯);
    writer.flush();
    writer.close();
    Log.v(MyApp的,二次getoldschedules称之为);
    getOldSchedules();
}公共无效getOldSchedules()抛出IOException    的FileInputStream FIS = openFileInput(schedules.txt);
    InputStreamReader的读者=新的InputStreamReader(FIS);    的char [] = INPUTBUFFER新的char [32];
    reader.read(INPUTBUFFER);
    字符串数据=新的String(INPUTBUFFER);
    Log.v(MyApp的,在文件中的数据读取:+数据);    reader.close();
}


解决方案

据我所知,没有什么错,立即与你的储蓄code。究其原因的onSaveInstanceState 是没有得到所谓的,因为它是在这种情况下错误的工作工具。这种方法被称为只有当一个活动是由系统的终止与带回来的意图的。从Android文档:


  

时的onPause()和的onStop()被调用的一个例子,而不是这种方法是当用户从活动B导航回活动答:没有必要第B调用的onSaveInstanceState(捆绑),因为该特定实例将不能被恢复,因此,系统避免了称之为


使用后退按钮浏览从活动之遥,是上述方案的一个例子 - 活动被销毁,所以国家并不需要保存,便于以后恢复。 的onSaveInstanceState 是小UI的事情,比如其中有复选框中检查他们设计了,还是在字段中输入文本 - 不是持久性的数据存储。你应该考虑将保存调入的onPause (如果它的快速),的onStop 的onDestroy

Whenever I press the back button my app doesn't call onSaveInstanceState() and I can't save data. I'm trying to make a scheduler app and I need to save schedules already set even after I press the back button. I want to dynamically edit a ListView by adding new data from the source file to a stringArray. The problem I'm having is that the file schedules.txt is not saving. Whenever the program opens a new activity the file is now blank.

This is the code I have so far:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_schedules);
    ListView listview = (ListView) findViewById(R.id.scheduleListView);
    adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, schedulesList); 
    listview.setAdapter(adapter);
    Log.v("myapp", "currentlist of files associated with this program are: " + fileList());

    try {

        FileOutputStream fout = openFileOutput("schedules.txt", MODE_PRIVATE);
        Log.v("myapp", "FileOutputStream ran");

    } catch (FileNotFoundException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

try {
       Log.v("myapp", "first get old schedules call");
       getOldSchedules();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}


public void editFile(String format) throws IOException {

    Log.v("myapp", "editFile ran");
    FileOutputStream fout = openFileOutput("schedules.txt", MODE_PRIVATE);
    OutputStreamWriter writer = new OutputStreamWriter(fout);

    writer.write("hello alex");
    writer.flush();
    writer.close();
    Log.v("myapp", "secondary getoldschedules call");
    getOldSchedules();
}

public void getOldSchedules() throws IOException{

    FileInputStream fis = openFileInput("schedules.txt");
    InputStreamReader reader = new InputStreamReader(fis);

    char[] inputbuffer = new char[32];
    reader.read(inputbuffer);
    String data = new String(inputbuffer);
    Log.v("myapp", "data in file reads: " + data);

    reader.close();


}

解决方案

As far as I can tell, there's nothing immediately wrong with your saving code. The reason onSaveInstanceState is not getting called is because it's the wrong tool for the job in this case. That method is called only when an Activity is killed by the system with the intent of bringing it back. From the Android documentation:

One example of when onPause() and onStop() is called and not this method is when a user navigates back from activity B to activity A: there is no need to call onSaveInstanceState(Bundle) on B because that particular instance will never be restored, so the system avoids calling it.

Using the back button to navigate away from the Activity is an example of the above scenario - the Activity is being destroyed, so the state doesn't need to be saved for later recovery. onSaveInstanceState is designed more for small UI things, like which checkboxes have checks in them, or the text entered in fields - not persistent data storage. You should consider placing the save call into onPause (if it's quick), onStop, or onDestroy.

这篇关于如何保存数据永久所以后来我可以访问它的Andr​​oid?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆