Skip to content

Commit

Permalink
Store model to shared preference onPause and restore it in onResume
Browse files Browse the repository at this point in the history
  • Loading branch information
gliechtenstein committed Jan 10, 2017
1 parent ff46973 commit f6aa1b5
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions app/src/main/java/com/jasonette/seed/Core/JasonViewActivity.java
Expand Up @@ -4,6 +4,7 @@
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.IntentFilter; import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.Color; import android.graphics.Color;
import android.graphics.Typeface; import android.graphics.Typeface;
Expand Down Expand Up @@ -49,6 +50,8 @@
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;

import static android.R.attr.action;
import static com.bumptech.glide.Glide.with; import static com.bumptech.glide.Glide.with;


public class JasonViewActivity extends AppCompatActivity{ public class JasonViewActivity extends AppCompatActivity{
Expand Down Expand Up @@ -215,6 +218,26 @@ protected void onPause() {
LocalBroadcastManager.getInstance(this).unregisterReceiver(onError); LocalBroadcastManager.getInstance(this).unregisterReceiver(onError);
LocalBroadcastManager.getInstance(this).unregisterReceiver(onCall); 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(); super.onPause();
} }


Expand All @@ -227,6 +250,24 @@ protected void onResume() {
LocalBroadcastManager.getInstance(this).registerReceiver(onError, new IntentFilter("error")); LocalBroadcastManager.getInstance(this).registerReceiver(onError, new IntentFilter("error"));
LocalBroadcastManager.getInstance(this).registerReceiver(onCall, new IntentFilter("call")); 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) { if (!firstResume) {
onShow(); onShow();
Expand Down

0 comments on commit f6aa1b5

Please sign in to comment.