0

This question is an off-shoot of a difficult problem that I am working on. One of the suggestion is to call the constructor of the parent activity. Does anyone know how I might do that? I mean, I have an activity called DogActivity and then I have ParentActivity such that DogActivity extends ParentActivity. Normally I use onCreate to communicate with parent activities as in super.onCreate(savedInstanceState) for instance. So I am at a loss of how I would call the constructor of a parent activity. Especially where I need to pass data through the constructor. How do I know android would call the same instance of the parent when it needs to deal with callbacks and lifecycle methods? Please see the original question for reference if you need to.

Community
  • 1
  • 1
learner
  • 11,490
  • 26
  • 97
  • 169
  • 2
    I'm pretty sure the person who posted the answer to your question isn't familiar with Android development... if you were to create your own constructor like that, then the framework would no longer know how to construct activity instances when it needs to and you would get runtime errors. I don't think that is the answer you are looking for. – Alex Lockwood Oct 07 '14 at 20:11
  • What you are asking for makes no sense in Android. Your design is wrong. What are you trying to do? Why would you want to call the constructor of a super class? Are you aware that you should NEVER create activities anyway other than via an `Intent'? – Simon Oct 07 '14 at 20:21
  • @Simon Chill. The OP is only asking this question because some guy with 16k rep suggested it. – Alex Lockwood Oct 07 '14 at 20:23
  • Ah go it. So if we strip away the noise, the question is, how I can I call a super class method from a child class and know which instance called it? Well, I guess the question makes sense but I still smell right problem, wrong question. @learner, in my opinion, you need to reword this to a simple statement of intent and a simple question. – Simon Oct 07 '14 at 20:25
  • Thanks AlexLockwood. @Simon if you refer to the linked question you might see why I asked this one, as AlexLockwood noticed. The person who answered me has a lot of points here on SO. So I assume that this may be something about android that I do not understand (after all, I certainly don't know everything). Also I find a link talking about using constructors in androids: http://stackoverflow.com/questions/3302177/android-activity-constructor-vs-oncreate. Anyway, I suppose my humility got the better of me. I didn't mean to ask a non-sensical question. I just wanted to double check. – learner Oct 07 '14 at 20:26
  • Yeh, I got it now. It makes sense for super class to know which class type called them but generally, not which instance. Without a simple statement of what you want, it's hard to know but probably, passing some static data to the method to identify the instance (by static, I don't mean static visibility, but static in nature) or, something as simple as super.someMethod(this); – Simon Oct 07 '14 at 20:30

1 Answers1

0
public class DogActivity extends ParentActivity {

    public DogActivity() {
        super("Something", new SomeObject());   // This is calling the super's constructor
    }
}

However on Android it is rare that you'd have an occasion where you need to call the parents constructor. So I have to say you are probably doing it wrong.

Blundell
  • 75,855
  • 30
  • 208
  • 233