0

I am noob in Android. Suppose I have two Activity in my android app.

Activity A and B, currently I am on Activity A then on click on one button I am now Activity B. Here From Activity B I want to update some view with updated data that is in Activity A. I got updated data in Activity B so Should I use here LocalBroadCastReceiver for this or anything ?? so that When I press back then app should show me updated data in Activity A

Should we use our custom callback here to update the UI view data of Activity A from Activity B ??

Mick
  • 1,179
  • 5
  • 14
  • 24

2 Answers2

2

No, you shouldn't use BroadcastReceiver for that. The method depends on the size of data you want to transfer from Activity B to Activity A. If it's quite small, you can start Activity B with startActivityForResult and then get data back at the onActivityResult method (in Activity B you should use setResult once you are done). If the size of data is quite big, it's better to use DB for storing it instead of keeping it in memory.

nikis
  • 11,166
  • 2
  • 35
  • 45
  • What about If I there are four activities A->B->C->D then I am getting updated data in D then want to set that data in TextView of A. then should I use `onActivityResult` ? – Mick Apr 14 '14 at 14:43
  • @Mick in such a case you should use `DB`, definitely. You can can make a cascade of `startActivityForResult`, but I think it's weird. – nikis Apr 14 '14 at 14:46
  • Suppose I get only one word from web api in D then for this I should use DB , How this can be good approach I don't think so to use here DB – Mick Apr 14 '14 at 14:48
  • @Mick well, for such small piece of data you can use `SharedPreferences` – nikis Apr 14 '14 at 14:50
  • Why here it is not good practice to use LocalBroadCastReceiver ? – Mick Apr 14 '14 at 14:54
  • @Mick because they are useless for sending data between activities. From docs: http://developer.android.com/reference/android/content/BroadcastReceiver.html If registering a receiver in your Activity.onResume() implementation, you should unregister it in Activity.onPause(). (You won't receive intents when paused, and this will cut down on unnecessary system overhead) – nikis Apr 14 '14 at 14:57
  • so where are LocalBroadCast Receiver use for communicating for what ? – Mick Apr 14 '14 at 14:58
1

okay , there are multiple ways to do it :-

  1. you can use a static variable to store you data that too of application level ( might not be a good approach ) check the value of vairable in onResume() and set it to the view.
  2. you can use sharedpreferences if your data is not that large , store the data in Activity B and fetch it on onResume() method of activity A.
  3. as @nikis has told you
  4. if your data is too large store it in db.

I dont think broadcastreceiver fits right in your scenario !!

r4jiv007
  • 2,974
  • 3
  • 29
  • 36