简体   繁体   中英

android activity class constructor working

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.?

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:

/**
 * Sample activity for passing parameters through a static constructor
 * @author Chase Colburn
 */
public class ParameterizedActivity extends Activity {

    private static final String INTENT_KEY_PARAM_A = "ParamA";

    private static final String INTENT_KEY_PARAM_B = "ParamB";

    /**
     * Static constructor for starting an activity with supplied parameters
     * @param context
     * @param paramA
     * @param paramB
     */
    public static void startActivity(Context context, String paramA, String paramB) {
        // Build extras with passed in parameters
        Bundle extras = new Bundle();
        extras.putString(INTENT_KEY_PARAM_A, paramA);
        extras.putString(INTENT_KEY_PARAM_B, paramB);

        // Create and start intent for this activity
        Intent intent = new Intent(context, ParameterizedActivity.class);
        intent.putExtras(extras);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Extract parameters
        Bundle extras = getIntent().getExtras();
        String paramA = extras.getString(INTENT_KEY_PARAM_A);
        String paramB = extras.getString(INTENT_KEY_PARAM_B);

        // Proceed as normal...
    }
}

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 the Intent 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 calls super() with the appropriate arguments, as such:

class BaseActivity extends Activity
{
    public BaseActivity(String param1, int param2)
    {
        // Do something with param1 and param2.
    }
    // Many more lines of awesomeness.
}

class DerivedActivity extends BaseActivity
{
    public DerivedActivity()
    {
        super("param1", 42);
    }
}

Naturally, if you need to generate the parameters to pass to BaseActivity() , you can simply replace the hard-coded values with function calls.

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

But you also can create very well a constructor of UserHome.

public class MainActivity extends Activity {
UserHome userHome = new UserHome(param1,param2);

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
userHome.yourMethod();
}}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM