0

I recently asked a question and one of the answers (this one) was to pass an Activity object via an Intent.

Is it really safe to do that (memory leaks?) and how can you achieve that? I tried passing it with intent.putExtra() but there is no method to pass an Activity.

Thanks in advance. I'm new to StackOverflow so if there is anything I've done wrong please tell me!

Michael Dodd
  • 10,102
  • 12
  • 51
  • 64
Neïl Rahmouni
  • 326
  • 2
  • 10
  • No, you shouldn't do that. What's your use case? – manfcas Jul 29 '18 at 20:42
  • @manfcas Yeah that's what i was thinking. Here is [the previous question I asked](https://stackoverflow.com/a/51579459/7507743). Thanks ! – Neïl Rahmouni Jul 29 '18 at 20:45
  • You should follow MidasLefko's answer for your other question. Passing an Activity through an Intent is a very bad idea. You want to either use `LocalBroadcastManager`, or you can use another library, such as `EventBus` to handle communication between two Activities. – advice Jul 29 '18 at 21:44
  • @Advice-Dog Thank you so much ! I was almost certain that it was a bad idea but I wanted to be sure. Is there a particular reason that is a bad idea? – Neïl Rahmouni Jul 30 '18 at 10:10
  • 2
    @Neïl The main reason you don't want to pass a handle to an Activity is because if you have any reference to it, the OS cannot clear out that memory. So you could easily have a memory leak, or a very memory hungry app. It's like passing a phone book to someone with your phone number circled within it, instead of just telling the your phone number. – advice Jul 30 '18 at 17:52

0 Answers0