sonix
sonix

Reputation: 243

Best practice: passing objects between Android activities

I was wondering what the best practice is to pass objects between activies? My logged out user is on a content item and should be able to get back to this item after the login process. Therefore I need to pass the content id between these activities

I see 2 basic options:

  1. Pass the content-id in the intent as URI (intent.putExtra()) and pass them between activities
  2. Save the content-id to the local storage and load it again after login

Are there any other options and best practices?

Upvotes: 7

Views: 651

Answers (1)

AC28
AC28

Reputation: 302

i would suggest using SharedPreferences which is like the option 2. which allows your to get the content-id(or string or json object) after the app is closed. you can also encrypt the content-id before put it in sharedPreferences

Besides intent (ram) and local storage (rom/sdcard , including database), i cant see any other option(locally).

Case 1: you need to resume activity after the app is closed
you should use local storage

Case 2: you don't need to resume activity after the app is closed

option 1:
  0. load the first activity
  1. start login_activity (startActivityForResult()) (do not call finish() )
  2. after login is done (call finish())
  3. activity is resumed (if login fail -> redirect to other activity )

option 2:
  1. create a public class with a data member to save the content-id/activity class
     (you may assign singleton design pattern) 

Upvotes: 2

Related Questions