Wednesday, August 29, 2012

[android-developers] Re: Get activity class instance

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+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate