2

I am currently building an Application which uses Dropbox API... (v2)

My Problem: I want to load some data from Dropbox only when the application is started and not every time when onCreate in my activity is called (e.g. caused by screen rotation). Well, I already found out, that you can extend the application class and write your code into it's onCreate Method to solve the problem... But the data I want to load on Application start has some influence on the ui of my Activity. My question is: how can I pass the data, which I loaded in application class, to my Activity?

1
  • 1
    Use 3rd party library like EventBus Commented Jun 22, 2016 at 15:20

2 Answers 2

2

Check out this great library for posting broadcasts in Android. It could be what you're looking for. You would set up your activity as a "listener", and your Application class as a publisher

https://github.com/greenrobot/EventBus

Sign up to request clarification or add additional context in comments.

4 Comments

This has nothing to do with posting notifications.
Sorry, coming from iOS lingo here. I meant notifications to mean "app-wide broadcast". I'll change the wording
That's more like it! :) Enjoy SO, since I see you're new here :)
Thanks! (and for helping me not mess up one of my first answers haha)
1

The simplest solution would be to a method in your application class to return the data. Then you could call ((MyApplication)getApplicationContext).getMyData() to retrieve it in your activity.

Of course the data may not be available depending on when you call that method. In that case you could register a listener in your application to pass the data back to your activity when it has been retrieved.

2 Comments

If I understand you right, that wouldn't solve the problem, because I had to call my method in activity class as well :/ And that means, that the method would be called always in onCreate of my Activity
Ohh I think I've just understand your idea. Isn't the data I load in my Application class removed when the system cleares the RAM? And thank you for your response!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.