質問

How can I set the Context (the first parameter in the Intent(Context, Class) constructor),

How can I set this context after creating an instance of an intent with it's empty constructor!?

UPDATE :

You mean that I can't set the context "alone"?

I need to set the context first, then after some steps I can set the class?

役に立ちましたか?

解決

You don't need to set the Context. An Intent doesn't need a Context. You only need to pass a Context in the particular variants of the constructor that also take a Class parameter (there are several available constructors):

Intent(Context packageContext, Class<?> cls)
Intent(String action, Uri uri, Context packageContext, Class<?> cls)

The reason that you need to pass a Context here is that the constructor uses the Context and Class parameters to set the Component in the Intent. To set the Component, the constructor needs to have the package name and the class name (both are of type String). It can get the class name from the Class parameter, and it uses the Context to get the package name.

You have several alternatives. You can use an empty Intent constructor and set the Component later using any of these methods:

setClassName (String packageName, String className)
setClassName (Context packageContext, String className)
setClass (Context packageContext, Class<?> cls)
setComponent (ComponentName component);

他のヒント

You can set context & class like below...

Intent intent = new Intent();
intent.setClass(your_context, YourActivity.class);

See this link for more details.

intent.setClass(Context, class) ?

EDIT

use your logic first, context will probably remain the same so decide which class you want to declare and then add it together with the context, if this is more complex issue please provide more information about your flow

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top