2

I have Recyclerview Fragment it work fine but when i change screen rotation , prograss bar not show any more , i know fragment distory all views , is there any way i can save prograss view obj in onsaveinstancestate or other any way to slove this problem ?

package com.example.ameerhamza6733.expressdaily.UI;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.res.Configuration;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ProgressBar;

import com.example.ameerhamza6733.expressdaily.MainActivity;
import com.example.ameerhamza6733.expressdaily.R;
import com.example.ameerhamza6733.expressdaily.Utils.Constant;
import com.example.ameerhamza6733.expressdaily.Utils.RssFeed;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.parser.Parser;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

/**
 * Demonstrates the use of {@link RecyclerView} with a {@link LinearLayoutManager} and a
 * {@link GridLayoutManager}.
 */
public class RecyclerViewFragment extends Fragment implements MainActivity.UpdateUI {

    private static final String TAG = "RecyclerViewFragment";
    private static final String KEY_LAYOUT_MANAGER = "layoutManager";
    private static final int SPAN_COUNT = 2;

    protected int indexofp = 0, indexOfPdash = 0;
    protected LayoutManagerType mCurrentLayoutManagerType;
    protected RecyclerView mRecyclerView;
    protected CustomAdapter mAdapter;
    protected RecyclerView.LayoutManager mLayoutManager;
    protected ArrayList<RssFeed> mDataset = new ArrayList<>();
    protected RssFeed rssFeed;
    protected Context context;
    protected String url = Constant.HOME_URL;
    protected ProgressBar progressBar;
    protected ImageButton mImageButton;
    protected boolean dataSetClrear=false;


    protected View view;

    @Override
    public void onArticleNavigationSeleted(String url) {
        this.url = url;

        mDataset.clear();



        dataSetClrear=true;


        Log.i(TAG, "Current URL" + url);
    }


    private enum LayoutManagerType {
        GRID_LAYOUT_MANAGER,
        LINEAR_LAYOUT_MANAGER
    }


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

        // Initialize dataset, this data would usually come from a local content provider or
        // remote server.


        context = getActivity();


    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.recycler_view_frag, container, false);
        rootView.setTag(TAG);

        this.view = rootView;
        // BEGIN_INCLUDE(initializeRecyclerView)
        mRecyclerView = (RecyclerView) rootView.findViewById(R.id.myRecylerView);
        progressBar = (ProgressBar) rootView.findViewById(R.id.progressBar);
        mImageButton = (ImageButton) rootView.findViewById(R.id.mImageButton);


        // LinearLayoutManager is used here, this will layout the elements in a similar fashion
        // to the way ListView would layout elements. The RecyclerView.LayoutManager defines how
        // elements are laid out.
        // mRecyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(this).color(Color.RED).sizeResId(R.dimen.divider).marginResId(R.dimen.leftmargin, R.dimen.rightmargin).build());
        mRecyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(getContext()).build());
        mLayoutManager = new LinearLayoutManager(getActivity());
        if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {

            mCurrentLayoutManagerType = LayoutManagerType.LINEAR_LAYOUT_MANAGER;
        } else {
            mCurrentLayoutManagerType = LayoutManagerType.GRID_LAYOUT_MANAGER;
        }

        if (savedInstanceState != null ) {
            // Restore saved layout manager type.
//            mCurrentLayoutManagerType = (LayoutManagerType) savedInstanceState
//                  .getSerializable(KEY_LAYOUT_MANAGER);
            progressBar.setVisibility(View.VISIBLE);
            mDataset = savedInstanceState.getParcelableArrayList(Constant.MY_DATA_SET_PARCE_ABLE_ARRAY_KEY);
            mAdapter = new CustomAdapter(mDataset);
            mRecyclerView.setAdapter(mAdapter);
            mAdapter.notifyDataSetChanged();

        } else {

            if (mDataset.isEmpty()) {
                progressBar.setVisibility(View.VISIBLE);
                initDataset();
            }
        }
        setRecyclerViewLayoutManager(mCurrentLayoutManagerType);


        return rootView;
    }

    /**
     * Set RecyclerView's LayoutManager to the one given.
     *
     * @param layoutManagerType Type of layout manager to switch to.
     */
    public void setRecyclerViewLayoutManager(LayoutManagerType layoutManagerType) {
        int scrollPosition = 0;

        // If a layout manager has already been set, get current scroll position.
        if (mRecyclerView.getLayoutManager() != null) {
            scrollPosition = ((LinearLayoutManager) mRecyclerView.getLayoutManager())
                    .findFirstCompletelyVisibleItemPosition();
        }

        switch (layoutManagerType) {
            case GRID_LAYOUT_MANAGER:
                mLayoutManager = new GridLayoutManager(getActivity(), SPAN_COUNT);
                mCurrentLayoutManagerType = LayoutManagerType.GRID_LAYOUT_MANAGER;
                break;
            case LINEAR_LAYOUT_MANAGER:
                mLayoutManager = new LinearLayoutManager(getActivity());
                mCurrentLayoutManagerType = LayoutManagerType.LINEAR_LAYOUT_MANAGER;
                break;
            default:
                mLayoutManager = new LinearLayoutManager(getActivity());
                mCurrentLayoutManagerType = LayoutManagerType.LINEAR_LAYOUT_MANAGER;
        }

        mRecyclerView.setLayoutManager(mLayoutManager);
        mRecyclerView.scrollToPosition(scrollPosition);
    }

    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
        // Save currently selected layout manager.

        // Save currently selected layout manager.
        //  savedInstanceState.putSerializable(KEY_LAYOUT_MANAGER, mCurrentLayoutManagerType);
//        savedInstanceState.putSerializable(KEY_LAYOUT_MANAGER, (Serializable) progressBar);

        savedInstanceState.putParcelableArrayList(Constant.MY_DATA_SET_PARCE_ABLE_ARRAY_KEY, mDataset);

        super.onSaveInstanceState(savedInstanceState);
    }

    /**
     * Generates Strings for RecyclerView's adapter. This data would usually come
     * from a local content provider or remote server.
     */
    private void initDataset() {

        if (mDataset.isEmpty()) {

            progressBar.setVisibility(View.VISIBLE);

            new LoadRssFeedsItems().execute("");
            Log.i(TAG, "" + mDataset.size());
            Log.i(TAG, "initDataset");
        }
    }


    public class LoadRssFeedsItems extends AsyncTask<String, Void, Void> {
        private String mTitle, mDescription, mLink, mPubDate;
        private String mCategory, mImageLn;
        private String date, mContent;
        private ProgressBar bar;

        public void setProgressBar(ProgressBar bar) {
            this.bar = bar;
        }
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            if (progressBar != null) {
                progressBar.setVisibility(View.VISIBLE);
            }
        }

        @Override
        protected void onProgressUpdate(Void... values) {

            progressBar.setProgress(1);
            super.onProgressUpdate(values);
        }

        @Override
        protected Void doInBackground(String... params) {

            Document rssDocument = null;
            try {
                rssDocument = Jsoup.connect(url).timeout(6000).ignoreContentType(true).parser(Parser.xmlParser()).get();

                Elements mItems = rssDocument.select("item");
                RssFeed rssItem;

                for (Element element : mItems) {


                    mTitle = element.select("title").first().text();
                    mDescription = element.select("description").first().text();
                    mLink = element.select("link").first().text();
                    mPubDate = element.select("pubDate").first().text();
                    mCategory = element.select("category").first().text();
                    mImageLn = element.select("media|content").attr("url").toString();
                    date = new SimpleDateFormat("dd-MMM-yyyy").format(new Date());
                    mContent = element.select("content|encoded").first().text();

                    mContent = Jsoup.parse(mContent).text();
                    indexofp = mDescription.indexOf(Constant.P);
                    indexOfPdash = mDescription.indexOf(Constant.P_DASH);
                    mDescription = mDescription.substring(indexofp + 3, indexOfPdash);

                    Log.i(TAG, "Item title: " + (mContent == null ? "N/A" : mContent));
                    Log.i(TAG, "Item title: " + (mTitle == null ? "N/A" : mTitle));
                    Log.i(TAG, "Item Description: " + (mDescription == null ? "N/A" : mDescription));
                    Log.i(TAG, "Item link: " + (mLink == null ? "N/A" : mLink));
                    Log.i(TAG, "Item data: " + (mImageLn == null ? "N/A" : mImageLn));
                    Log.i(TAG, "Item data: " + (mPubDate == null ? "N/A" : mPubDate));
                    Log.i(TAG, "system date: " + (date == null ? "N/A" : date));

                    rssFeed = new RssFeed(mTitle, mLink, mPubDate, mCategory, mLink, mDescription, mImageLn, context, mContent);
                    mDataset.add(rssFeed);

                }

            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            if (mDataset.isEmpty()) try {


                // mImageButton.setVisibility(View.VISIBLE);
                Snackbar mSnackbar = Snackbar.make(view, "انٹرنیٹ دستیاب نہیں ہے", Snackbar.LENGTH_INDEFINITE)
                        .setAction("دوبارہ کوشش کریں", new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
//                                Snackbar.make(getView(), "CheckIn Cancelled", Snackbar.LENGTH_LONG).show();

                                new LoadRssFeedsItems().execute("");


                            }
                        });
                mSnackbar.show();
            } catch (NullPointerException n) {
                n.printStackTrace();
            }
            else {


                progressBar.setVisibility(View.INVISIBLE);
                mAdapter = new CustomAdapter(mDataset);
                mRecyclerView.setAdapter(mAdapter);
                mAdapter.notifyDataSetChanged();


            }

        }

    }
}

here is my xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"


        android:id="@+id/myRecylerView" />

    <ProgressBar
        style="?android:attr/progressBarStyle"
        android:layout_marginTop="40dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/progressBar" />

    <FrameLayout
        android:id="@+id/mFramLaout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageButton
            android:layout_gravity="center"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:srcCompat="@drawable/ic_cloud_off_black_24dp"
            android:id="@+id/mImageButton"
            android:elevation="0dp"
            android:visibility="gone"
            android:background="#f1f1f1" />

    </FrameLayout>
</LinearLayout>
Hamza
  • 2,017
  • 1
  • 19
  • 40
  • try this one: http://stackoverflow.com/questions/18354569/maintaining-progress-bar-visibility-with-orientation-change – Just_someone Sep 26 '16 at 15:11

2 Answers2

0

You can use Bundle object to this purpose. In onDestroy() function add that line

@Override
public void onSaveInstanceState(Bundle outState)
{

    super.onSaveInstanceState(outState);

    outState.putInt("PROGRESS", progress);

}

When Fragment will be recreated you can get back this value using this code in your public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) function by adding this line:

if(savedInstanceState!=null)
    {
        progress = savedInstanceState.getInt("PROGRESS");
    }
W.Kurek
  • 299
  • 2
  • 11
-1

Saving a variable at onSaveInstanceState didn't work for me, since you won't get updates while the Activity is destroyed.

I ended up using a ResultReceiver inside a Fragment that doesn't get destroyed by using setRetainInstance(true).

A good article concerning this problem can be found here: https://www.androiddesignpatterns.com/2013/04/retaining-objects-across-config-changes.html

Also see my answer here: https://stackoverflow.com/a/54334864/6747171

Stephan
  • 1
  • 1