Yes, Parcelable Bundles are "the Android Way", but even the "Android Way" does not pretend to be the ideal solution for ALL inter- Application object passing problems; just most of them;) Why, Android Intents do not even use Bundles (extras) to pass all the data passed between Activities/Applications: it also uses Uris.
Rather, as others have already pointed out, there are multiple methods of doing this supported by Android, each has its own best practices. Based on the limited info the OP gave about the problem he has in mind, it is hard to say which is best for him. Of all the methods in this thread, there are only one or two that are quite likely not at all suitable. Even your proposed method could turn out to be unsuitable if the OP really has in mind a more general problem than he stated, such as putting the object in persistent storage. For as the API docs explain: "Parcel is NOT a general-purpose serialization mechanism. This class (and the corresponding Parcelable API for placing arbitrary objects into a Parcel) is designed as a high-performance IPC transport. As such, it is NOT APPROPRIATE TO PLACE ANY PARCEL DATA IN TO PERSISTENT STORAGE: changes in the underlying implementation of any of the data in the Parcel can render older data unreadable. (http://developer.android.com/reference/android/os/Parcel.html, emphasis mine)". That said, I have to agree that parcelable bundles should be among the first solutions considered, instead of the last, as it very nearly turned out in this thread (another poster already mentioned it, but neither as extensively nor as forcefully). If the OP does not need object persistence, just object passing, then using an Intent Extra is the right solution, well worth the time to learn about Parcel, Parcelable and Bundle also. I had to say the because I am sure that many people will be deterred from using the Parcelable Bundle solution for only that reason: there are so many classes to learn to know how to do this correctly and feel confident that you are doing the right thing: Parcel, Bundle, Parcelable, Binder, IBinder... On Jul 8, 12:05 am, Robert Green <[email protected]> wrote: > You guys, this is all wrong. Yes, you can just shove stuff into some > static object somewhere but that's not the android way. Doing that > may cause undesirable side-effects. > > Inter-activity communication in Android is done via parcelable > bundles. > > Read the page on > intents:http://developer.android.com/intl/de/reference/android/content/Intent... > > An intent can take extras, in the form of putExtra(Int), > putExtra(String), etc.. You can also pass a Bundle using > putExtras(Bundle). You use the getIntent().getIntExtra(), etc to get > the data back out on the called activity.. Here's kinda how it looks: > > class Activity1 { > private void startOtherActivity() { > Intent i = new Intent(this, Activity2.class); > i.putExtra("Key1", "Value1"); > i.putExtra("Key2", 20); > startActivity(i); > } > > } > > class Activity2 { > protected void onStart() { > Intent i = getIntent(); > String extra1 = i.getStringExtra("Key1"); > int extra2 = i.getIntExtra("Key2", 0); > } > > } > > Most application data passes just fine this way. You can also make > any class Parcelable and pass it as an extra or in a Bundle. This > allows you to pretty much pass any data so long as you write the > parcelable implementation for it (which is really fairly easy and > straight-forward once you learn it). > > Doing it this way allows for multiple activity instances to exist on > the stack without error. Going with the static object as suggested is > error-prone unless you have adequate protection code. > > On Jul 4, 11:39 pm, metal mikey <[email protected]> wrote: > > > You can do something like this (where i've written the code from > > memory and thus missed a lot of important stuff, no doubt): > > > public class MyApplication extends Application { > > SomeDataObject myDataObject; > > > public static class SomeDataObject { > > String someString; > > int someInt; > > } > > > public void onCreate() { > > myDataObject = new SomeDataObject(); > > myDataObject.someString = ""; > > } > > > } > > > public class ActivityA extends Activity { > > public void someFunction() { > > MyApplication.myDataObject.someString = "hello"; > > } > > > } > > > public class ActivityB extends Activity { > > public void someFunction() { > > localString = MyApplication.myDataObject.someString; > > } > > > } > > > On Jul 4, 10:34 pm, Thomas Frick <[email protected]> wrote: > > > > Dear All, > > > > I have two activities. > > > Activity A creates an object and stores some data in this object. > > > After that, activity B ist started. In activity B I have to use the > > > object created in activity A. > > > > What is the best way to pass the object from activity A to B? > > > > I think it is not possible to store an object in the > > > SharedPreferences, isn't it? > > > > Is it a good way to call the object in activity A directly from > > > activity B? > > > > Thanks for your help. > > > Thomas > > -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/android-developers?hl=en

