3

I have two activities - activity A starts activity B. Then activity B loads some stuff from database, and visualizes the data. It takes some time (5-10sec) to fully initialize all the stuff in B.

My problem is that if I press back button in B, it destroys B activity (calls onDestroy) and returns to activity A. I still want to return to activity A, but without destroying B, because if I launch B again, I have to wait again before it does all the stuff (which is annoying). Is this possible somehow?

Thanks for your help!

Jaa-c
  • 5,017
  • 4
  • 34
  • 64

5 Answers5

5

You can use Fragments, but what you really want to do is cache the information. Otherwise you'll reload on device rotation anyways (unless you use Fragments and setRetainInstance to true, but this can cause other headaches).

I would use a CursorLoader on Activity B's onCreate. onLoadFinished you cache the result and setup your UI. On subsequent loads the results are cached and you forego the loader. What's more, using the CursorLoader will alert you if the underlying database results change.

yarian
  • 5,922
  • 3
  • 34
  • 48
  • That's the right answer, as the one of @Orabîg, you will never be able to ask android to keep an activity alive. Android can garbage collect whatever activity when it is under memory pressure. – Snicolas Mar 25 '13 at 21:41
  • Thanks, I'll look into Fragments. I know I can't force android not to kill the activity. I just expect the average case when you go back to A and a few minutes after you want to go to B again... – Jaa-c Mar 25 '13 at 21:45
  • Again, fragments may not even work and if they do they come with some subtleties that you should be well aware of. See http://developer.android.com/reference/android/app/Fragment.html#setRetainInstance%28boolean%29 I would much rather recommend you look into Loaders. – yarian Mar 25 '13 at 21:49
  • 1
    Also you need to handle screen orientation. Even in that situation activity is destroyed and recreated. – Raghunandan Mar 25 '13 at 21:49
  • @Raghunandan: Thanks, I know about that. But I don't expect orientation to change very often, I'm concerned about the average cases.. – Jaa-c Mar 25 '13 at 21:54
2

I think you do not see the problem from the right point of view.

I'd say that you can't prevent B to be destroyed if Android wants to (because it's up to it to handle the activities). However, you have the choice to move all the long initialisation in a third class which can be persisted all the time. So you should make this init phase independant from your B activity.

I think that putting all this in an attribute of your Application class would be a good idea.

Orabîg
  • 11,718
  • 6
  • 38
  • 58
  • Thanks, but I don't want to manage a third class where I would temporarily persist the initialized data, since there are more instances of B with different data (and the data update periodically) so it would be quite complicated... – Jaa-c Mar 25 '13 at 21:49
  • Do as you wish, but I think that you won't have many choices. And I don't get where your argument explains that you cannot use an external class. I wish you'll find the best solution for you. – Orabîg Mar 25 '13 at 22:35
0

Maybe you can use AndroidFragments

Shmuel
  • 3,916
  • 2
  • 27
  • 45
0

Use SharedPreferences to store key-value pairs then use getSharedPreferences() to retrieve them.

sebster
  • 1,322
  • 2
  • 18
  • 29
0

When you press back button Activity b is destroyed. It is how android works. I would not try to override the default functionality

Once you get data from data base you can cache then some where and load it.

http://developer.android.com/guide/topics/data/data-storage.html. Check the topic under Saving cache files.

Raghunandan
  • 132,755
  • 26
  • 225
  • 256