The existing answers (pass the data in the Intent
passed to startActivity()
) show the normal way to solve this problem. There is another solution that can be used in the odd case where you're creating an Activity that will be started by another app (for example, one of the edit activities in a Tasker plugin) and therefore do not control the Intent
which launches the Activity
.
You can create a base-class Activity
that has a constructor with a parameter, then a derived class that has a default constructor which calls the base-class constructor with a value, as so:
class BaseActivity extends Activity
{
public BaseActivity(String param)
{
// Do something with param
}
}
class DerivedActivity extends BaseActivity
{
public DerivedActivity()
{
super("parameter");
}
}
If you need to generate the parameter to pass to the base-class constructor, simply replace the hard-coded value with a function call that returns the correct value to pass.