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:
Are there any other options and best practices?
We can send the data using putExtra() method from one activity and get the data from the second activity using the getStringExtra() method.
One way to pass objects in Intents is for the object's class to implement Serializable. This interface doesn't require you to implement any methods; simply adding implements Serializable should be enough. To get the object back from the Intent, just call intent.
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 storageCase 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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With