0

I am fetching data from the web using volley and displaying the result in TextViews and an NetworkImageView.

I have succeeded in saving and restoring the states of the textviews but I ran into a problem with the NetworkImageView.

FruitDetails

public class FruitDetails extends AppCompatActivity {

    private final String TAG = "FruitDetails";


    TextView fruitTitle, fruitContent;
    NetworkImageView authorImg;
    ImageLoader AuthImgLoader;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fruit_details);

        fruitTitle = (TextView) findViewById(R.id.dfruit_title);
        fruitContent = (TextView) findViewById(R.id.dfruit_content);
        authorImg = (NetworkImageView) findViewById(R.id.author_img);

        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }

        if (savedInstanceState != null) {
            fruitTitle.setText(savedInstanceState.getString("fruitTitle"));
            fruitContent.setText(savedInstanceState.getString("fruitContent"));
        } else {

                loadFruit();

        }
    }

    private void loadFruit() {
        Log.d(TAG, "loadFruit called");

        final ProgressBar progressBar;
        progressBar = (ProgressBar) findViewById(R.id.progress_circle);
        progressBar.setVisibility(View.VISIBLE);


        int news_id = getIntent().getIntExtra("FruitId", -1);
        Log.d(TAG, "You clicked fruit id " + fruits_id);

        final JsonObjectRequest jsonObjReq = new JsonObjectRequest( DetailConfig.GET_DURL + news_id, null,


                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        Log.d("Debug", response.toString());

                        //Dismissing progressbar;
                        if (progressBar != null) {
                            progressBar.setVisibility(View.GONE);
                        }

                        //Calling method to parse json array
                        parseFruit(response);
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        VolleyLog.d("", "Error: " + error.getMessage());
                    }
                });

        //Creating request queue
        RequestQueue requestQueue = Volley.newRequestQueue(this);

        //Adding request to queue
        requestQueue.add(jsonObjReq);
    }

    //This method will parse json data of fruit
    private void parseFruit(JSONObject jsonObject) {

        Log.d(TAG, "Parsing fruit array");


            try {
                String title = jsonObject.getString(DetailConfig.TAG_DFRUIT_TITLE);
                fruitTitle.setText(Html.fromHtml(title));

                JSONObject pAuthor = jsonObject.getJSONObject("author");
                String authorimg = pAuthor.getString("avatar");

                AuthImgLoader = VolleyRequest.getInstance(getApplicationContext()).getImageLoader();
                AuthImgLoader.get(authorimg, ImageLoader.getImageListener(authorImg, R.drawable.ic_author, R.drawable.ic_author));
                authorImg.setImageUrl(authorimg, AuthImgLoader);


                String content = jsonObject.getString(DetailConfig.TAG_DFRUIT_CONTENT);
                fruitContent.setText(Html.fromHtml(content));

            } catch (JSONException w) {
                w.printStackTrace();
            }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putString("fruitTitle", fruitTitle.getText().toString());
        outState.putString("fruitContent", fruitContent.getText().toString());
        //outState.putInt("authorImg", authorImg // I got into problem here
    }

}

While I couldn't restore that of the NetworkImageView at all, the fruitContent has an anomaly: when I rotate the screen, the styling (e.g bold) of the text is lost i.e it's returned to normal.

Edit I discovered that the reason why the styling is lost is because I was setting and getting it as string; is there anyway I can use Parcelables?

X09
  • 3,827
  • 10
  • 47
  • 92

1 Answers1

0

Save the url to your bundle in onSaveInstanceState and restore it in onRestoreInstanceState. That assumes the image doesn't need to change urls with rotation to one of a more appropriate size.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • That means that the image would be redownloaded? Right? I don't want that. – X09 Apr 16 '16 at 22:11
  • If you're using Volley, you should have an ImageCache and a DiskCache preventing that. Otherwise why use Volley? – Gabe Sechan Apr 17 '16 at 00:32
  • The url is stored in `authoring` but I can't seem to access that in onSaveInstanceState – X09 Apr 17 '16 at 00:42