开发者

Android: Pass complex data structures through activities or services

Hi I want to pass complex data structures from my Service class to Activity class.

I create class Serializable :

        class SaveMe implements Serializable {

        private static final long serialVersionUID = 1L;

        static final int test = 1234;

        private int user;

        private String name;

        private double score;


        public SaveMe() {}
    }

And I put it in Bundle

Bundle bundle = new Bundle();
bundle.putSeria开发者_JAVA百科lizable("serializable", new SaveMe());

HOW can I send this information to Activity class ?

I try with this code :

Intent mapIntent = new Intent(INTENT_MAP);     
mapIntent.putExtras(bundle);

And than I want to send to BroadcastReciver using:

 sendBroadcast(mapIntent);

But I receive force error!

Should I use BroadcastReceiver to pass this data structure to my activity?

How to send to another activity?

Thanks


You can store any data required globally by extending the Application class and including a getter to return the existing SaveMe stored there. You can access this from your activity with

private MyApplication application;

@Override
   public void onCreate(final Bundle savedInstanceState) {
                ........
   this.application = (MyApplication) this.getApplication();
                ......
}

// elsewhere in your code
SaveMe mySaveMe = application.getSaveMe();

This article explains very clearly how to do this: http://www.screaming-penguin.com/node/7746 You can ignore the stuff about the AsyncTask (although I'm sure you will find a need for that at some point) and just concentrate on the part about extending the application class.


One option is to basically steal the idea of the R class. You have a final class that you can reference from anywhere. I'm not sure if this is a good idea or not. Not many people try this

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜