简体   繁体   English

Android:保存对象onPause()并读取对象onResume()

[英]Android: save object onPause() and read object onResume()

I was not able to save an object(custom) to a file. 我无法将对象(自定义)保存到文件。

The process of my app is to saving the object onPause() and getting the object onResume(). 我的应用程序的过程是保存对象onPause()并获取对象onResume()。

1) I have two activities (Activity A and Activity B)
2) I am saving and reading the object in Activity A 
3) Activity A call Activity B on Button click.

Here are the steps to replicate my problem: (Activity A launches by reading an object from file.) 以下是复制我的问题的步骤:(活动A通过从文件中读取对象来启动。)

1) Click on button on Activity A.
2) Save custom object to file in onPause method of Activity A.
3) Activity B launches
4) Click on Action bar back button.
5) Read the object from file saved in onResume() method of Activity A.
6) Again click on the button on Activity A.
7) Saves custom object to file in onPause() method of Activity A.
8) Activity B launches.
9) Click on back button of device.
10) Tries to read the object from file in onResume() method of Activity A.

Please note: the file that I am saving and reading is at same location. 请注意:我要保存和读取的文件在同一位置。

Here at step 10, the object read is incorrect. 此处在步骤10,读取的对象不正确。 I suspect that it is at step 7, object was not saved properly. 我怀疑是在步骤7中,对象未正确保存。

Can someone help with saving object to file and reading a file to object? 有人可以帮助保存对象到文件以及读取文件到对象吗?

Correct me if I am wrong. 如果我错了,请纠正我。 As per the Android document, onSaveInstanceState() and onRestoreInstanceState() are not suitable method to save. 根据Android文档,onSaveInstanceState()和onRestoreInstanceState()不适合保存。

here is the manifest: 这是清单:

    <activity
        android:name=".ActivityA"
        android:configChanges="orientation|keyboardHidden|screenSize"
        android:label="@string/app_name"
        >
    </activity>
    <activity
        android:name=".ActivityB"
        android:configChanges="orientation|keyboardHidden|screenSize"
        android:label="@string/app_name" >
    </activity>

Saving and reading object here (ActivityA): 在此处保存和读取对象(ActivityA):

Customer customer;
@Override
protected void onPause() {
    super.onPause();
    save(customer);
}

@Override
protected void onResume() {
    super.onResume();
    customer = read();
}

  private void save(Customer customer) {
    try {
        FileOutputStream fileOutputStream = openFileOutput("Customer.properties", Context.MODE_PRIVATE);
        Properties properties = new Properties();
        properties.put("CUSTOMER_NAME", customer.getName());
        properties.put("CUSTOMER_ID", customer.getId());
        properties.put("CUSTOMER_PLACE", customer.getPlace());

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
}

     private Customer read() {
     try {
          FileInputStream fileInputStream = openFileInput("customer.properties");
          Properties properties = new Properties();
          properties.load(fileInputStream);
          Customer customer = new Customer();
          customer.setName(properties.get("CUSTOMER_NAME"));
          customer.setId(properties.get("CUSTOMER_ID"));
          customer.setPlace(properties.get("CUSTOMER_PLACE"));
          return customer;
     } catch(Exception e) {
     }
   return null;
  }

在@override上,super.onPause()和super.onResume()总是首先出现,然后只有您才能放置您的代码。

you should close your input and output streams after you're done with them, otherwise you'll run into a concurrency problem 处理完输入和输出流后,应将其关闭,否则会遇到并发问题

  private void save(Customer customer) {
     ...
     fileOutputStream.close()
  }

  private Customer read() {
     ...
     fileInputStream.close()
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM