6

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

TKV
  • 2,533
  • 11
  • 43
  • 56
  • 1
    Why you don't want to use putExtra to pass the parameters, intead its used for passing the data from one Activity to Another. – Lalit Poptani Jan 04 '12 at 10:48

4 Answers4

9

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

Chase
  • 11,161
  • 8
  • 42
  • 39
  • @Chase: Although it was asked so long ago, I hope I shall get a response. I am trying to use this method but I get a NullPOinterException for the context parameter (this) in the call to launch the activity ParameterizedActivity. Any idea what could be wrong? – Monica Apr 13 '14 at 19:34
  • Be sure that the Context object passed in is not null. If you are holding on to a context reference, be sure that it is still valid. Remember that activities are destroyed and recreated on config changes such as orientation change. – Chase May 07 '14 at 01:34
3

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.

Trebor Rude
  • 1,904
  • 1
  • 21
  • 31
0

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

jennifer
  • 8,133
  • 22
  • 69
  • 96
0

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.

Dany's
  • 943
  • 1
  • 7
  • 17
  • 1
    UserHome is indeed just another class. But the instance of UserHome that you create WILL NOT be the one that the Android system uses. – Vikram Bodicherla Jan 04 '12 at 11:19