Skip to content
This repository was archived by the owner on Feb 26, 2023. It is now read-only.
This repository was archived by the owner on Feb 26, 2023. It is now read-only.

Support generics in @InstanceState #113

Description

@pyricau

Generics fields don't work yet with @InstanceState :

    @SaveOnActivityDestroy
    MyGenericSerializableBean<MySerializableBean> myGenericSerializableBean;

It generates the following compile error : Unrecognized type. Please let your attribute be primitive or implement Serializable or Parcelable.

We decided to merge pull request #101 anyway (issue #77), and we'll handle generics in a new branch.

Activity

  1. funk78 commented on Feb 20, 2012

    @funk78

    Is it the same thing with supporting generics for @EActivity in general?
    I cant use Android Annotations on a Generic Activity at all.

  2. pyricau commented on Mar 1, 2012

    @pyricau
    ContributorAuthor

    Hello @funk78. I think we just never thought of generic activities, because they are created by the Android container. Is that an abstract activity, or a normal activity ? What do you use that for ?

  3. funk78 commented on Mar 1, 2012

    @funk78

    Right now its an abstract class, because i needed a workaround for android annotations to use @UiThread and @background. But it should be a normal activity.

    I use it like this:

    public abstract class EndlessListActivity<T> extends ListActivity {
    
        public interface EndlessListActivityContentProvider<T> {
            List<T> getNextItems(Integer offset);
        }
    
        private final List<T>                           listData            = new ArrayList<T>();
    
        private boolean                                 isLoadingMoreDataFromService;
    
        private boolean                                 stopLoadingRecords  = false;
    
        private ArrayAdapter<T>                         listItemAdapter;
    
        private int                                     lastOffset          = -123;
    
        private EndlessListActivityContentProvider<T>   contentProvider;
    
        private MyToast                                 toast;
    
        public void afterViews(EndlessListActivityContentProvider<T> content, MyToast toast) {
            this.contentProvider = content;
            this.toast = toast;
            this.listItemAdapter = initListAdapter();
            setListAdapter(this.listItemAdapter);
            callLoadListItemsTaskInBackground(0);
            toast.showToast(R.string.toast_initial_download_triggered);
            initOnScrollReloadListener();
        }
    
        public void attachToListView(List<T> records) {
            this.listData.addAll(records);
            EndlessListActivity.this.listItemAdapter.notifyDataSetChanged();
            this.isLoadingMoreDataFromService = false;
    
            if (records.size() < Cfg.listChunkSize || this.listData.size() >= Cfg.ListViewMaxSize) {
                this.stopLoadingRecords = true;
            } else {
                callLoadMoreIfSpaceAvailableInUiThread();
            }
        }
    
        public abstract void callAttachToListViewInUiThread(List<T> items);
    
        public abstract void callLoadListItemsTaskInBackground(int offset);
    
        public abstract void callLoadMoreIfSpaceAvailableInUiThread();
    
        public T getItemAtPosition(int position) {
            return this.listData.get(position);
        }
    
        public List<T> getListData() {
            return this.listData;
        }
    
        public View getListItem(View convertView, int layoutId) {
            View listItem;
            if (convertView == null) {
                LayoutInflater inflater = getLayoutInflater();
                listItem = inflater.inflate(layoutId, null, true);
            } else {
                listItem = convertView;
            }
            return listItem;
        }
    
        public abstract ArrayAdapter<T> initListAdapter();
    
        public void initOnScrollReloadListener() {
            getListView().setOnScrollListener(new OnScrollListener() {
    
                @Override
                public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                    EndlessListActivity<T> activity = EndlessListActivity.this;
                    if (activity.isLoadingMoreDataFromService || activity.stopLoadingRecords) {
                        return;
                    }
    
                    if (visibleItemCount == totalItemCount) {
                        loadMore(totalItemCount);
                        return;
                    }
                    if (totalItemCount - Cfg.listChunkSize < firstVisibleItem + visibleItemCount) {
                        loadMore(totalItemCount);
                        return;
                    }
                }
    
                @Override
                public void onScrollStateChanged(AbsListView view, int scrollState) {}
    
                private void loadMore(int totalItemCount) {
                    EndlessListActivity.this.isLoadingMoreDataFromService = true;
                    callLoadListItemsTaskInBackground(totalItemCount);
                }
            });
        }
    
        public void loadListItemsTask(Integer offset) {
            if (this.lastOffset == offset) {
                return;
            }
    
            this.lastOffset = offset;
            List<T> items = null;
    
            items = this.contentProvider.getNextItems(offset);
    
            if (items == null) {
                this.toast.showToast(R.string.toast_download_records_error);
            }
    
            callAttachToListViewInUiThread(items);
        }
    
        public void loadMoreIfSpaceAvailable() {
            int count = getListView().getAdapter().getCount();
            int lastVisiblePos = getListView().getLastVisiblePosition();
            if (lastVisiblePos == count) {
                callLoadListItemsTaskInBackground(count);
            }
        }
    }
  4. pyricau commented on Mar 15, 2012

    @pyricau
    ContributorAuthor

    Ok, thanks @funk78, could you create a separate issue for that :) ?

    @matboniface, regarding generics and @InstanceState, is that something you could fix ?

  5. mathieuboniface commented on Mar 15, 2012

    @mathieuboniface
    Contributor

    I already started to work on that issue.

    It's not so easy to handle it, I hope I can provide a fix next week.

  6. added a commit that references this issue on Apr 13, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions