How to save ArrayList in an Bundle Object

前端 未结 1 693
天命终不由人
天命终不由人 2021-01-20 21:33

Hi
I need help in finding a way to store

 ArrayList> 

in an Bunble object so that i can retrieve t

1条回答
  •  佛祖请我去吃肉
    2021-01-20 22:17

    first you must have a static holder:

    private static class Holder{
            private ListimageList = new ArrayList();
        }
    

    second, when orientation start, you must return the object you want to retrieve after the orientation:

    @Override
        public Object onRetainNonConfigurationInstance() {
            return holder;
        }
    

    at last, when you create the 'new' activity must call getLastNonConfigurationInstance(). ANdroid will return your holder with your List.

    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
                    holder = (Holder) getLastNonConfigurationInstance();
    }
    

    you can find a more extensive explanation here: Faster Screen Orientation.

    cheers

    0 讨论(0)
提交回复
热议问题