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.html

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 <coref...@gmail.com> 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 <frick...@gmail.com> 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 android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to