When considering the case with android activity, the first method to work is its onCreate
method..right?
Suppose i want to pass 2 parameters to android activity class say UserHome
. For that am creating the constructor of activity class UserHome
and accepting the params.
But when we are calling an activity we are not initializing the Activity class, we are just creating an intent of UserHome
class.
Then how can we pass params to that activity from another activity without using intent.putExtra("keyName", "somevalue");
usage.
Experts please clarify how we can cover a situation like this.?
We can pass the value from parent activity to child activity using the bundled collection and shared preference. 1. Shared Preference 2. Bundle Collection
Passing data or parameter to another Activity Android
Not sure why you would not want to use the intent params. That is what they are there for. If you need to pass the same parameters from different places in your application, you could consider using a static constructor that builds your intent request for you.
For example:
You can then launch your activity by calling:
ParameterizedActivity.startActivity(this, "First Parameter", "Second Parameter");
I can see one situation where you'd be unable to use the standard method of passing the parameters via the
Intent
: When you're creating an activity that will be launched by another app (say, the edit activity of a Tasker plugin) and, therefore, do not have control over theIntent
that will launch your activity.It's possible to create an
Activity
that accepts parameters in its constructor. The trick to using it, though, is not to use it directly, but to use a derived class with a default constructor that callssuper()
with the appropriate arguments, as such:Naturally, if you need to generate the parameters to pass to
BaseActivity()
, you can simply replace the hard-coded values with function calls.But you also can create very well a constructor of UserHome.
Why do you think that is not possible to initialize a contructor?..MainActivity is a class like any other, just that extends Activity, but also keeps the properties of a class, so that can have, constructors, methods, members.