MDP
MDP

Reputation: 4267

How to pass Views between Classes

I've been working on android for few months, during my spare time, so probably i'm going to ask stupid questions.

In a class, I create an object, for example a button, and I'd like to 'reach' it from others classes.

Of course i could do it this way:

public class CreateButton{

      public void createButton(){

         Button myButton = new Button(context);         

        //I can "pass" my object to another class this way
        ManageButton manageButton = new ManageButton(myButton);

        // or this way
        manageButton.writeButtonTextMethod(myButton); 

     }
}

or this way

public class CreateButton{

      public Button createButton(){

        Button myButton = new Button(context);
        return myButton;

      }

}


public class ManageButton{

      public void writeButtonTextMethod(){

        CreateButton createButton = new CreateButton()     
        Button myButton = createButton.createButton();
        myButton.setText("W"); 

      }   
}

I'm wondering if there's a way to 'reach' myButton, created in createButton.class directly from ManageButton.class (and other classes).

With the code above, I have to 'call' createButton.class from ManageButton.class or vice versa to be allowed to manage myButton.

I can do it making myButton static, but it's not correct make view static.

For example, i can easily reach variables created in a class that extends Application. Is there something similar for views?

Upvotes: 2

Views: 761

Answers (3)

Ewoks
Ewoks

Reputation: 12435

Not quite sure why would u like to share one button between two activities?! It is either button in ActivityA or ActivityB? No need, or use to do it..You shouldn't do it.

EDIT: This is more Java Basics topic then Android.. And from what I understood, u call "class" something that IMHO is method (because u can't CALL the class and because what did you described).

Best advices I can give u here:

  1. Study Java basics (methods, classes, objects, constructors, inheritance..),
  2. Study Android basics (simple examples with 1 activity til u understand it good),
  3. Make MyButton Class which will extend standard button,
  4. Create button(s) and change their properties

This few tasks will make u busy for some time. Take it easy because it will be easier later with complicated apps. Hope I helped you. Cheers

EDIT2: In last example u wrote (in comments to this answer), nothing make sense because as we wrote u before u don't share button between two activities. Pause! Read it again: You should't share button between two Activies. Questions u asked are not clear because for A and Z u can't say just class and "share" button between them. Which class? no line of code written so it is really difficult to figure out what u want.. :s

LAST LAST EDIT: Man.. Please understand that u r missing basics of Java. No bad intentions here, just try to guide u and tell u exactly what u should study. Why? Because it doesn't surprise me that you are confused to explain what u mean when classes are with one method named exactly the same.. They are just objects "doing something", u want to "manage" them by adding variable in constructor which doesn't exist etc.. Learn, next time write immediately code to save time for all of us. Cheers and enjoy learning

Upvotes: 0

Raghav Sood
Raghav Sood

Reputation: 82543

Views are not meant to be shared across Activities. Even if you do disregard this and share them, you're more than likely to run into a WindowManager$BadTokenException, which essentially means you can't alter Views outside of its parent Activity.

However, to answer your question in a more general way about sharing non-view objects, the Application class is the right way to go. However, making the objects static is not the right way to do. Instead, make them instance variables, and access them in in your various activities by using:

Application application:

//Below line is in onCreate()
application = this.getApplication();

After that, simple access it using

application.myObject; //Or you could use getters and setters. Your choice.

Upvotes: 0

CommonsWare
CommonsWare

Reputation: 1006614

Much people/guides/articles claim is possible and correct sharing objects (and fields) between activities by using Application, this way:

Such people are idiots, as myButton will introduce a memory leak unless this is done very carefully.

So, I'm still not sure how i should share objects (TextView, Button, ImgaeView etc.) among different activities.

You don't share widgets between activities. At most, you share model data between activities (or, better yet, pointers to centrally-stored model data).

In a Web app, you do not share DOM nodes between Web pages, as that is not possible. At most, you share data passed as GET parameters and the like between Web pages, or you store data in local central storage (cookies, local storage, IndexedDB, etc.). Android activities are like Web pages -- they are loosely coupled.

If you have pieces of your UI that are so tightly coupled that they absolutely need to share widgets, they should not be separate activities.

Upvotes: 2

Related Questions