Android Open Source - GridListViewAdapters Retained Data Fragment






From Project

Back to project page GridListViewAdapters.

License

The source code is released under:

Apache License

If you think the Android project GridListViewAdapters listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/**
 * Copyright 2014-present Biraj Patel/*from  w  w  w.j a v  a  2 s  .  co m*/
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License. You may obtain a
 * copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */
package com.birin.listgridadapter.datasetup;

import java.util.ArrayList;

import android.app.Activity;
import android.app.Fragment;
import android.os.AsyncTask;
import android.os.Bundle;

/**
 * Helper fragment class which will be used to hold data across configuration
 * changes of activity life-cycle as mentioned in below link
 */
// http://www.androiddesignpatterns.com/2013/04/retaining-objects-across-config-changes.html
public class RetainedDataFragment extends Fragment {

  private DummyDataGeneratorCallback mCallbacks;
  private DummyTask mTask;
  private ArrayList<Employee> listData = new ArrayList<Employee>();
  private boolean isTaskRunning;
  public static final String KEY_MAX_ITEMS = "KEY_MAX_ITEMS";

  /**
   * Hold a reference to the parent Activity so we can report the task's
   * current progress and results. The Android framework will pass us a
   * reference to the newly created Activity after each configuration change.
   */
  @Override
  public void onAttach(Activity activity) {
    super.onAttach(activity);
    mCallbacks = (DummyDataGeneratorCallback) activity;
  }

  /**
   * This method will only be called once when the retained Fragment is first
   * created.
   */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Retain this fragment across configuration changes.
    setRetainInstance(true);
    runAddDataTask();
  }

  /**
   * Set the callback to null so we don't accidentally leak the Activity
   * instance.
   */
  @Override
  public void onDetach() {
    super.onDetach();
    mCallbacks = null;
  }

  public ArrayList<Employee> getListData() {
    return listData;
  }

  public boolean isTaskRunning() {
    return isTaskRunning;
  }

  private int getMaxAllowedItems() {
    int maxAllowed = Integer.MAX_VALUE;
    Bundle extras = getArguments();
    if (null != extras && extras.containsKey(KEY_MAX_ITEMS)) {
      maxAllowed = extras.getInt(KEY_MAX_ITEMS);
    }
    return maxAllowed;
  }

  public void runAddDataTask() {
    if (isTaskRunning() == false && canLoadMoreData()) {
      if (null != mTask) {
        mTask.cancel(true);
        mTask = null;
      }
      // Create and execute the background task.
      mTask = new DummyTask();
      mTask.execute();
    }
  }

  public boolean canLoadMoreData() {
    return listData.size() < getMaxAllowedItems();
  }

  /**
   * A dummy task that performs some (dumb) background work and proxies
   * progress updates and results back to the Activity.
   * 
   * Note that we need to check if the callbacks are null in each method in
   * case they are invoked after the Activity's and Fragment's onDestroy()
   * method have been called.
   */
  private class DummyTask extends
      AsyncTask<Void, Integer, ArrayList<Employee>> {

    @Override
    protected void onPreExecute() {
      isTaskRunning = true;
      if (mCallbacks != null) {
        mCallbacks.onDataGeneratorTaskExecuting();
      }
    }

    /**
     * Note that we do NOT call the callback object's methods directly from
     * the background thread, as this could result in a race condition.
     */
    @Override
    protected ArrayList<Employee> doInBackground(Void... ignore) {
      try {
        Thread.sleep(1500);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
      return generateNewDummyDataAndAddToList();
    }

    @Override
    protected void onCancelled() {
      isTaskRunning = false;
      if (mCallbacks != null) {
        mCallbacks.onDataGeneratorTaskCancelled();
      }
    }

    @Override
    protected void onPostExecute(ArrayList<Employee> newDataList) {
      isTaskRunning = false;
      if (mCallbacks != null) {
        mCallbacks.onNewDummyDataGenerated(newDataList);
      }
    }

    private ArrayList<Employee> generateNewDummyDataAndAddToList() {
      int currentSize = listData.size();
      ArrayList<Employee> newDummyData = new ArrayList<Employee>();
      for (int i = currentSize; i < (currentSize + 20); i++) {
        newDummyData.add(new Employee(i));
      }
      listData.addAll(newDummyData);
      return newDummyData;
    }

  }

  /**
   * Callback interface through which the fragment will report the task's
   * progress and results back to the Activity.
   */
  public interface DummyDataGeneratorCallback {

    public void onDataGeneratorTaskExecuting();

    public void onDataGeneratorTaskCancelled();

    public void onNewDummyDataGenerated(ArrayList<Employee> newDataList);

  }

}




Java Source Code List

com.birin.cursorgridadapter.base.BaseCursorGridActivity.java
com.birin.cursorgridadapter.base.BaseEmployeeCursorGridAdapter.java
com.birin.cursorgridadapter.datasetup.CursorRetainingFragment.java
com.birin.cursorgridadapter.datasetup.TestContentProviderSqlHelper.java
com.birin.cursorgridadapter.datasetup.TestContentProvider.java
com.birin.cursorgridadapter.demo1.FixedCursorItems.java
com.birin.cursorgridadapter.demo2.CardClickHandlingEmployeeCursorGridAdapter.java
com.birin.cursorgridadapter.demo2.CardClickHandlingFixedCursorItems.java
com.birin.cursorgridadapter.demo3.ChildAndCardClickHandlingEmployeeCursorGridAdapter.java
com.birin.cursorgridadapter.demo3.ChildAndCardClickHandlingFixedCursorItems.java
com.birin.cursorgridadapter.demo4.FixedCursorItemsRotationSupport.java
com.birin.cursorgridadapter.demo5.UnlimitedCursorItemsRotationSupportAutoLoadMore.java
com.birin.cursorgridadapter.demo6.UnlimitedCursorItemsRotationClickToLoadMore.java
com.birin.gridlistviewadapters.BaseGridAdapter.java
com.birin.gridlistviewadapters.Card.java
com.birin.gridlistviewadapters.CursorFilter.java
com.birin.gridlistviewadapters.CursorGridAdapter.java
com.birin.gridlistviewadapters.ListGridAdapter.java
com.birin.gridlistviewadapters.RowViewHolder.java
com.birin.gridlistviewadapters.dataholders.CardDataHolder.java
com.birin.gridlistviewadapters.dataholders.CardPositionInfo.java
com.birin.gridlistviewadapters.dataholders.RowDataHolder.java
com.birin.gridlistviewadapters.utils.ChildViewsClickHandler.java
com.birin.gridlistviewadapters.utils.GridDataStructure.java
com.birin.gridlistviewadapters.utils.MaxCardsInfo.java
com.birin.gridlistviewadapters.utils.OnLoadMoreRequestListener.java
com.birin.gridlistviewadapters.utils.PositionCalculator.java
com.birin.gridlistviewadapters.utils.ViewHolderPositionTagger.java
com.birin.gridlistviewadaptersdemo.BaseDemoMenuList.java
com.birin.gridlistviewadaptersdemo.CursorDataDemos.java
com.birin.gridlistviewadaptersdemo.JavaUtilListDataDemos.java
com.birin.gridlistviewadaptersdemo.ParentDemoMenuList.java
com.birin.gridlistviewadaptersdemo.common.CharacterDrawable.java
com.birin.gridlistviewadaptersdemo.common.Constants.java
com.birin.gridlistviewadaptersdemo.common.EmployeeCardViewHolder.java
com.birin.gridlistviewadaptersdemo.common.RandomInfoGenerator.java
com.birin.listgridadapter.base.BaseEmployeeListGridAdapter.java
com.birin.listgridadapter.base.BaseListGridActivity.java
com.birin.listgridadapter.datasetup.Employee.java
com.birin.listgridadapter.datasetup.RetainedDataFragment.java
com.birin.listgridadapter.demo1.SimplestListGridAdapterUsageDemoActivity.java
com.birin.listgridadapter.demo2.FixedListItems.java
com.birin.listgridadapter.demo3.CardClickHandlingEmployeeListGridAdapter.java
com.birin.listgridadapter.demo3.CardClickHandlingFixedListItems.java
com.birin.listgridadapter.demo4.ChildAndCardClickHandlingEmployeeListGridAdapter.java
com.birin.listgridadapter.demo4.ChildAndCardClickHandlingFixedListItems.java
com.birin.listgridadapter.demo5.FixedListItemsRotationSupport.java
com.birin.listgridadapter.demo6.UnlimitedListItemsRotationSupportAutoLoadMore.java
com.birin.listgridadapter.demo7.UnlimitedListItemsRotationSupportAutoLoadMoreMax100Items.java
com.birin.listgridadapter.demo8.UnlimitedListItemsRotationSupportClickToLoadMore.java