0

I need to share variables and objects (TextViews, Buttons etc), that i create in my first activity with all my others activities and classes.

For example:

STEP 1: in my main activity (class A), i call a method (in class B) that creates dinamically several buttons.

STEP 2: When users click one of these buttons i call a method (in class C) that check if users "did somethings".

STEP 3: If users did somethings, from Class C i call a method (in class D) that disables all the buttons i created in class B.

Question: What's the right way to reach, from class D, the buttons created in class B

How should i handle it? I have to use this object and variables even inside simple classes that aren't activity, so i can't use Application.

MDP
  • 4,177
  • 21
  • 63
  • 119
  • References to TextViews and Buttons could retrieved from layout using findViewById method. take a look [here](http://stackoverflow.com/questions/4011152/how-to-get-reference-to-a-button-created-in-a-custom-dialog-using-a-xml-layout?answertab=active#tab-top) – Rohit Feb 08 '13 at 10:02
  • from a long few years of experience on Android I can for almost sure that you have the basic concept all wrong. I really can't help you more without more knowledge on your specific app – Budius Feb 08 '13 at 10:03
  • @Rohit I can't use findViewById because i need to reach some objects in classes that aren't activities. – MDP Feb 08 '13 at 10:22
  • @Budius i've been studying java and android for some months, that's why i ask stupid questons :D Anyway, independently from my app, i'm trying to understand how to handle that situation. For example, in my main activity (class A), i call a method (in class B) that creates dinamically several buttons. When i click one of these buttons i call a method (in class C) that check if users "did somethings". If users did somethings, from Class C i call a method (in class D) that disables all the buttons i created in class B. What's the righe way to reach, from class D, the buttons created in class B. – MDP Feb 08 '13 at 10:29
  • you can declare them static all the variable and object you can easily call with class name – Yogesh Tatwal Feb 08 '13 at 10:54
  • 1
    @Yogesh Tatwal I create static objects/variables in my previous app,but one user tells me this: **your approach of keeping a final static boolean variable in your start Activity is fatal and wrong. This does not work on Android. Although there are reasons when you shouldn't use static variables, I think that's fine for such purposes as startup analysis of environment aspects. The right way to do this on Android is using an Application class, perform the analysis there and store it in variables there.** That's why i thought it wasn't a good choise to use static variables and objects – MDP Feb 08 '13 at 11:06
  • @MatteoDepasquali I've never said the word 'stupid'. Everybody starts learning from some point and I'm just questioning you do review your logic. As pointed by your other question, do not use static on everything. There're common Java tricks like using interfaces to warn interested parties about events, it's something you can look into. I'll write an answer as a possible suggestion of what I could capture from your explanation. – Budius Feb 08 '13 at 12:22

1 Answers1

0

here's a suggestion based on your explanation. Please remember that I'm typing it all by hearts and it's likely to have a few typos.

STEP 1: in my main activity (class A), i call a method (in class B) that creates dinamically several buttons.

public class A extends Activity implements OnClickListener{
      private ArrayList<Buttons> mybuttons = new ArrayList<Butons>

      // ... at some point you call B
      mybuttons.addAll(b.createButton(this));
           // createButtons should receive an OnClickListener as parameter to set on the buttons
           // createButtons returns a List<Buttons> that you add to your list.
           // note that it's NOT a static list, and that the list is part of the Activity, the Activity can hold reference to its views without problem

}

STEP 2: When users click one of these buttons i call a method (in class C) that check if users "did somethings".

STEP 3: If users did somethings, from Class C i call a method (in class D) that disables all the buttons i created in class B.

  // because A implements the OnClickListener, the OnClick is called inside A
  onClick(View v){
        // call the C to check. and make it return a boolean (true or false)
       if(c.CheckStuff()){
           // disable your buttons.
           for(Button btn:mybuttons) {  btn.setEnabled(false);  }
        }
  }

Question: What's the right way to reach, from class D, the buttons created in class B

answer: you don't have to, to be fair, if D is only disabling the buttons is only that ONE line I typed. If it's doing more stuff, you can pass mybuttons to D, but I urge to not keep a permanent reference of it inside D, that's because D is not part of the activity life-cycle.

Budius
  • 39,391
  • 16
  • 102
  • 144
  • Thank for helping me!! Before reading your answer i tried to find documentation on the internet and i found this on [http://developer.android.com](http://developer.android.com/guide/faq/framework.html) **An alternate way to make data accessible across Activities/Services is to use public static fields and/or methods. You can access these static fields from any other class in your application. To share an object, the activity which creates your object sets a static field to point to this object and any other activity that wants to use this object just accesses this static field.** – MDP Feb 08 '13 at 14:35
  • It seems correct use simple **static** field. Anyway I'll look into interfaces as you suggest, to try understand if your solution is better/easier than use stati stuff – MDP Feb 08 '13 at 14:37
  • That's not what the text is saying. The text is saying that use static fields and/or methods is one of the ways of making DATA accessible across your app. Let me repeat **DATA**. Any View, Widget or Context (or anything else that holds the context) should never be made static because it will invariably lead to memory leaks. – Budius Feb 08 '13 at 16:33
  • oh, you are right. I'm going to follow your example! :) Thank you – MDP Feb 11 '13 at 07:00