android activity class constructor working

2020-02-26 12:28发布

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

4条回答
一夜七次
2楼-- · 2020-02-26 13:01

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

查看更多
再贱就再见
3楼-- · 2020-02-26 13:13

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");

查看更多
三岁会撩人
4楼-- · 2020-02-26 13:13

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.

查看更多
Melony?
5楼-- · 2020-02-26 13:14

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.

查看更多
登录 后发表回答