AsyncTaskLoader when screen turns



  • The Annex with AsyncTaskLoader takes news from the JSON website, puts them in RecycleView. It's working. But there's a problem-- when the screen turns, it's reloading the data. To work on this point, I redo it through Fragment.

    created FeedLoader and launched forceLoad() in the method onCreate in Fragment - so that it does not start every turn.

    Why doesn't the RetrieveFeedFragment make collbets onLoadFinished and others? How do we make sure nothing changes when the screen turns, except View just turns around?

    I can't do this for a week. I'll be grateful for advice, help or excuse!

    activity_main.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    

    <FrameLayout
    android:id="@+id/fragment_news"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1">
    </FrameLayout>

    </LinearLayout>

    news_list_fragment

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#dec9c9">

    <RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    &lt;LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_centerInParent="true"
        android:orientation="vertical"
        android:id="@+id/progressBarLinearLayout"&gt;
    
        &lt;ProgressBar
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/progressBar"
            android:layout_gravity="center_horizontal"/&gt;
    
        &lt;TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/retrieve_feed"
            android:id="@+id/progressBarText"
            android:layout_below="@id/progressBar"
            android:textColor="#164e66"
            android:textSize="18dp"
            android:paddingTop="10dp"/&gt;
    
    &lt;/LinearLayout&gt;
    
    
    &lt;FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"&gt;
    
        &lt;android.support.v7.widget.RecyclerView
            android:id="@+id/listViewNews"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"&gt;
        &lt;/android.support.v7.widget.RecyclerView&gt;
    
    &lt;/FrameLayout&gt;
    

    </RelativeLayout>

    </LinearLayout>

    MainActivity

    RetrieveFeedFragment fragment;;

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if(savedInstanceState == null){
        fragment = new RetrieveFeedFragment();
        getSupportFragmentManager().beginTransaction().add(R.id.fragment_news, fragment, "retrieve_feed_tag").commit();
        Log.d("myLogs", "New Fragment in Activity");
    }
    else{
        fragment = (RetrieveFeedFragment) getSupportFragmentManager().findFragmentByTag("retrieve_feed_tag");
        Log.d("myLogs", "Get old Fragment by tag in Activity");
    }
    

    }

    FeedLoader

    public class FeedLoader extends AsyncTaskLoader<ArrayList<RSSItemData>>{

    /* Не нагромождаю кодом, вставляю основное */
    
    public ArrayList&lt;RSSItemData&gt; loadInBackground(){
    
    /* Парсим данные, возвращаем listData */
    
    return listData;
    
    }
    

    }

    RetrieveFeedFragment

    public class RetrieveFeedFragment extends Fragment
    implements android.app.LoaderManager.LoaderCallbacks<ArrayList<RSSItemData>>{

    private RecyclerView rvNews;
    LinearLayout progBarLinearLayout;
    ProgressBar progBar;
    TextView progBarText;
    FeedLoader FeedLoader;
    RSSAdapter RSSAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setRetainInstance(true);
    FeedLoader = new FeedLoader(getActivity());
    FeedLoader.forceLoad();
    Log.d("myLogs", "Fragment onCreate");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {

    Log.d("myLogs", "Fragment onCreateView");
    
    View v = inflater.inflate(R.layout.news_list_fragment, null);
    
    rvNews = (RecyclerView) v.findViewById(R.id.listViewNews);
    LinearLayoutManager llm = new LinearLayoutManager(getActivity());
    rvNews.setLayoutManager(llm);
    rvNews.setHasFixedSize(true);
    
    rvNews = (RecyclerView) v.findViewById(R.id.listViewNews);
    
    progBarLinearLayout = (LinearLayout) v.findViewById(R.id.progressBarLinearLayout);
    progBar = (ProgressBar) v.findViewById(R.id.progressBar);
    progBarText = (TextView) v.findViewById(R.id.progressBarText);
    
    return v;
    

    }

    @Override
    public Loader<ArrayList<RSSItemData>> onCreateLoader(int i, Bundle bundle) {
    return null;
    }

    @Override
    public void onLoadFinished(Loader<ArrayList<RSSItemData>> loader, ArrayList<RSSItemData> listData) {
    RSSAdapter = new RSSAdapter(listData, getActivity());
    rvNews.setAdapter(RSSAdapter);
    progBarLinearLayout.setVisibility(LinearLayout.GONE);
    }

    @Override
    public void onLoaderReset(Loader<ArrayList<RSSItemData>> loader) {

    }

    }



  • I don't think it's necessary to check null. savedInstanceStateAnd the fragment.

    That's the method. onCreate Class MainActivity For:

    if(savedInstanceState == null){ ... }

    Make:

    if (fragment == null) { ... }


    How do we make sure nothing changes when the screen turns, except View just turns around?

    We could use the file. manifest.mxl Add the line android:configChanges="orientation|screenSize" You'll be activated. But this method is not recommended to use both simple developers and Android developers.

    EDIT:

    Use the method. setRetainInstance♪ If it's true, it's not going to cause the methods to reconstruct the fragment. onDestroy and onCreateand there will be no new version of the class Fragment

    Try the first way, at least you can use the second.


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2