0

I have 2 activities

Activity A has a registered receiver that updates a static generic list.

Activity B accesses the static generic list in Activity A.

The problem is:

If Activity A ended (killed by the system), the registered receiver stops, which makes the data in the static generic list unreliable, as it depends on the registered receiver to be updated.

And the biggest problem that the static generic list doesn't get terminated (null / zeroed) along with the registered receiver, though both of them created by the same activity.

So there's no way for me to know that my static generic list has become unreliable to reload it all over with fresh data when Activity B gets created.

Also there's no way - listener - to listen to the registered receiver termination, where I'd have terminated the generic static list, and check it in Activity B creation, if it's null, I'll reload a fresh data.

And as you know it's not guaranteed that onDestroy will get called when the system terminates the activity, otherwise I'd have terminated the generic static list.

I even added a static Boolean var (mAlive) to Activity A that has a default value (false) and becomes (true) in onCreate, in hope that when Activity A gets destroyed, (mAlive) will become false (the default value in its declaration), which I could check in Activity B to know that the registered receiver is terminated and the data is unreliable and reload it again, but it turns out, that even destroying Activity A, doesn't reset static Boolean var (mAlive) to its default value (false).

So, any suggestions please, I got a brain freeze trying to find a solution in the past 2 weeks.

Thank you

Jack
  • 693
  • 1
  • 7
  • 25

3 Answers3

1

You seem to be greatly misunderstanding the Activity Lifecycle.

Only one Activity at a time can be running. The current one is suspended when a new one starts and you cannot access directly anything contained in one Activity from a different Activity. Static variables can hold simple data but that is not recommended as a way to avoid dealing with the Activity Lifecycle.

To pass data between activities, you use Intents and Extras. How do I pass data between Activities in Android application?

If you want two Activities to share the same data, you could use a Service that contains the data which can be accessed from both Activities.

Kuffs
  • 35,581
  • 10
  • 79
  • 92
  • How can I access the generic list inside the Service from an Activity? – Jack May 16 '18 at 16:36
  • @Thank you Kuffs, really appreciated. And the Binder class is really pretty helpful, in your opinion, if I use a public static generic list within the service and accessed it directly, does it have side effects / down sides compared to Binder? – Jack May 16 '18 at 19:03
  • Personally I would avoid static variables but sometimes there is a valid use case. https://stackoverflow.com/questions/2475978/using-static-variables-in-android – Kuffs May 17 '18 at 05:14
0

You can fire a Broadcast(BroadcastReceiver Example) from your desired activity's onDestroy() method. Afterwards Receive broadcast in your required activity.

buzzingsilently
  • 1,546
  • 3
  • 12
  • 18
  • Thank you, but haven't I mentioned in the question that onDestroyed is not guaranteed to get called when the system destroys the activity? – Jack May 16 '18 at 13:19
0

You can put your data into a separate object so you don't dipend on your activity A's lifecycle. You can see an example here at Finder section

I think you could make a parent activity extended by both your Activity A and B. In your parent activity you could register your receiver into its onResume method and then unregister it into its onPause method. By this way you should have always your receiver registered to the current activity.

Then if you have read the previous link, if you make a Finder responsible to keep your fresh data and you reference it into your parent Activity (so you have a reference into both Activity A and Acitivity B) you should be sufficiently sure that a variable referenced by the current active activity can't be destroyed by the system.

I'm not sure to have understood what you need, let me know

firegloves
  • 5,581
  • 2
  • 29
  • 50
  • Thank you ... And what about the registered receiver that updates the data? will it also be part of this separate object? and is it guaranteed that if this object got destroyed by the system both the registered receiver and the generic static list will be terminated with each other? – Jack May 16 '18 at 13:23
  • And what kind of object do you recommend? – Jack May 16 '18 at 13:26
  • Thank you firegloves, I'll check the first suggestion and its link and get back to you ... as the second suggestion, I forgot to mention that I tried it too (Base activity that both activities derived from it, but I faced the same problem, as either the receiver will be registered twice, or to an instance of one of the activity, which leads to the same problem). – Jack May 16 '18 at 13:43
  • Edit, your second suggestions that I said that it won't be suitable, might be possible, as you using onResume and onPause which is different than what I did .. I'll give it a try too. – Jack May 16 '18 at 13:45
  • I've suggested only one thing, you should try with all the stuff I suggested if you want – firegloves May 16 '18 at 15:11