>
>  I have 2 classes that extend "Activity", MyActivityA.java and
> MyActivityB.java.
>  How do I start MyActivityB from MyActivityA and access its public
> variables?
>

You don't...

 {
>  MyActivityB actB=new MyActivityB();
>  actB.varX="Hello";
>  actB.startActivity(new Intent(this, MyActivityB.class));
>  }
>  But this doesn't work. It throws an exception.
>

As it should...

By searching online I allways see starting an activity like this:
>  ...
>  {
>  startActivity(new Intent(this, MyActivityB.class));
>  //But now how do I get an instance to MyActivityB class that was started?
>  }
>

You don't...

 I would really prefer to access the MyActivityB class public variables.
>

That is a really, really, really bad idea...

 I know of passing parameters in the Intent using putExtra(), but I am
> looking for an alternative because of the complexity of the project.
>

If your project is complex then that is all the more reason why you should
stick to the best programming practices.  As sparky said, you can use
SharedPreferences or extend Application to hold the necessary data.  In
general though, it is usually recommended to write a Singleton class rather
than extend Application.  Really, the best option (in most cases) is to
pass the parameters via your intent.

Thanks,
Justin Anderson
MagouyaWare Developer
http://sites.google.com/site/magouyaware


On Wed, Aug 29, 2012 at 6:05 AM, sparky <spar...@google.com> wrote:

> If your project is complex, then having Activities reading each
> others' member variables will only make it worse.  You need
> encapsulation and clean interfaces.
>
> For small collections of data, maybe you could get away with extending
> Application and putting them there.  Shared Preferences would also be
> okay.  If your data model is a bit more complex, then you probably
> need to give it a class of its own, and persist it by attaching it to
> the Application, or access it through a Service, or create a database,
> something like that.
>
> But saying, "I want to forego best practices because my project is
> complex," sounds like a good way to get into trouble.
>
>
>
> On Aug 29, 10:00 am, Filipe <filipe.ma...@gmail.com> wrote:
> > Hi,
> >
> > I am new to Android, so I still am having some basic dificulties.
> >
> >  I have 2 classes that extend "Activity", MyActivityA.java and
> > MyActivityB.java.
> >  How do I start MyActivityB from MyActivityA and access its public
> > variables?
> >  Comming from C++ I would expect something like:
> >  ...
> >  {
> >  MyActivityB actB=new MyActivityB();
> >  actB.varX="Hello";
> >  actB.startActivity(new Intent(this, MyActivityB.class));
> >  }
> >
> > But this doesn't work. It throws an exception. By searching online I
> > allways see starting an activity like this:
> >  ...
> >  {
> >  startActivity(new Intent(this, MyActivityB.class));
> >  //But now how do I get an instance to MyActivityB class that was
> started?
> >  }
> >  I would really prefer to access the MyActivityB class public variables.
> >  I know of passing parameters in the Intent using putExtra(), but I am
> > looking for an alternative because of the complexity of the project.
> >
> > Thanks
>
> --
> 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
>

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