Permalink
Browse files

Store model to shared preference onPause and restore it in onResume

  • Loading branch information...
1 parent ff46973 commit f6aa1b5bbdd0d1329505fdc059348eb6d960c6f2 @gliechtenstein gliechtenstein committed Jan 10, 2017
Showing with 41 additions and 0 deletions.
  1. +41 −0 app/src/main/java/com/jasonette/seed/Core/JasonViewActivity.java
@@ -4,6 +4,7 @@
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
+import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Typeface;
@@ -49,6 +50,8 @@
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
+
+import static android.R.attr.action;
import static com.bumptech.glide.Glide.with;
public class JasonViewActivity extends AppCompatActivity{
@@ -215,6 +218,26 @@ protected void onPause() {
LocalBroadcastManager.getInstance(this).unregisterReceiver(onError);
LocalBroadcastManager.getInstance(this).unregisterReceiver(onCall);
+ // Store model to shared preference
+ SharedPreferences pref = getSharedPreferences("model", 0);
+ SharedPreferences.Editor editor = pref.edit();
+
+ JSONObject temp_model = new JSONObject();
+ try {
+ if (model.url != null) temp_model.put("url", model.url);
+ if (model.jason != null) temp_model.put("jason", model.jason);
+ if (model.rendered != null) temp_model.put("rendered", model.rendered);
+ if (model.state != null) temp_model.put("state", model.state);
+ if (model.var != null) temp_model.put("var", model.var);
+ if (model.cache != null) temp_model.put("cache", model.cache);
+ if (model.params != null) temp_model.put("params", model.params);
+ if (model.session != null) temp_model.put("session", model.session);
+ editor.putString("temp", temp_model.toString());
+ editor.commit();
+ } catch (Exception e) {
+ Log.d("Error", e.toString());
+ }
+
super.onPause();
}
@@ -227,6 +250,24 @@ protected void onResume() {
LocalBroadcastManager.getInstance(this).registerReceiver(onError, new IntentFilter("error"));
LocalBroadcastManager.getInstance(this).registerReceiver(onCall, new IntentFilter("call"));
+ SharedPreferences pref = getSharedPreferences("model", 0);
+ if(pref.contains("temp")){
+ String str = pref.getString("temp", null);
+ try {
+ JSONObject temp_model = new JSONObject(str);
+ model.url = temp_model.getString("url");
+ model.jason = temp_model.getJSONObject("jason");
+ model.rendered = temp_model.getJSONObject("rendered");
+ model.state = temp_model.getJSONObject("state");
+ model.var = temp_model.getJSONObject("var");
+ model.cache = temp_model.getJSONObject("cache");
+ model.params = temp_model.getJSONObject("params");
+ model.session = temp_model.getJSONObject("session");
+ } catch (Exception e) {
+ Log.d("Error", e.toString());
+ }
+ }
+
if (!firstResume) {
onShow();

0 comments on commit f6aa1b5

Please sign in to comment.