开发者

How to save List<UserDefinedClass> of between Activity switching?

开发者 https://www.devze.com 2023-03-25 04:47 出处:网络
How to save L开发者_开发问答ist<UserDefinedClass> of between Activity switching ? I get data in onClickView function. Does UserDefinedClass need toimplements some interface ?I believe the best w

How to save L开发者_开发问答ist<UserDefinedClass> of between Activity switching ? I get data in onClickView function. Does UserDefinedClass need to implements some interface ?


I believe the best way would be to keep the list in the Application context.

In any of your activitys call yourApp using:

((yourApp)getApplication()).//any public function

public yourApp extends Application{
    private List<UserDefinedClass> yourList;

    //create functions to manipulate the list example:
    public void add(UserDefinedClass a){
        //add this to list
    }

    //finally function to access list
    public List<UserDefinedClass> getList(){
        return yourList;
    }
}

//example:
List<UserDefinedClass> aList = ((yourApp)getApplication()).getList();


It is best to only pass an identifier to the next Activity instead of passing your entire list.

UserDefinedClass should not implement some interface, rather UserDefinedClass should be the class type of the List. In other words, every item in your list should be of UserDefinedClass. That can be a String or even a custom class.


Yes you can pass it if it implements Parcelable. However if its a large amount of data a database or file may work out better for you.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号