The working class of the Android activity class.

When considering the case of android activity, the first working method is its onCreate method.

Suppose I want to pass 2 parameters to the Android activity class say UserHome . To do this, I create a constructor for the UserHome activity UserHome and accept params.

But when we call an activity, we do not initialize the Activity class, we just create the intent of the UserHome class.

Then, how can we pass the parameters of this activity from another action without using intent.putExtra("keyName", "somevalue"); .

Experts explain how we can cover this situation.

+4
source share
4 answers

Not sure why you don't want to use intent parameters. For this they are. If you need to pass the same parameters from different places in the application, you might consider using a static constructor that builds your request for intentions for you.

For instance:

 /** * 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... } } 

Then you can start your activity by calling:

ParameterizedActivity.startActivity(this, "First Parameter", "Second Parameter");

+7
source

I see one situation where you cannot use the standard method of passing parameters through Intent : when you create an action that will be launched by another application (say, the edit action is a Tasker plugin) and therefore do not have control over the Intent that will trigger your activity .

It is possible to create an Activity that takes parameters in its constructor. However, the trick with using it is not to use it directly, but to use a derived class with a standard constructor that calls super() with the corresponding 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 parameters to switch to BaseActivity() , you can simply replace the encoded values ​​with function calls.

+3
source

We can pass the value from parent activity to child activity using the collected collection and general preferences. 1. General preferences 2. Collection of packages

Passing data or parameters to another Android activity

0
source

But you can also very well create a UserHome constructor.

 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 it is impossible to initialize a constructor? .. MainActivity is a class like any other, just extending the Activity, but also preserving the properties of the class, so it can have constructors, methods, members.

0
source

Source: https://habr.com/ru/post/1389250/


All Articles