cdbeelala89
cdbeelala89

Reputation: 2146

Android Activity: how to init variable with constructor? Do I need putExtra?

I have

public abstract class SuperClassA extends Activity
{
   protected String s;

   public SuperClassA(String s)
   { this.s = s }

   ...etc...
}

Now I do

  startActivity(new Intent(this, SubClassA.class));

Is there some way to call the activity while using the constructor? Otherwise the field s is not initialized. Or do I really have to do this with putExtra?

Upvotes: 0

Views: 346

Answers (1)

vRallev
vRallev

Reputation: 5040

Each non abstract activity must have a public default constructor with no arguments. The system tries to call this constructor to create a new activity. In your case this constructor is not found and you (should) receive an exception.

The same implies for Service, BroadcastReceiver, Fragments, etc...

So yes, you need to use an argument Bundle to pass the String argument.

Upvotes: 1

Related Questions