I am using the following code to move to another Activity :
Intent intent = new Intent();
String test = "Navigate";
intent.setClassName(this.context,test);
intent.putExtra("params", params);
((Activity) context).startActivity(intent);
But I am unable to start the Navigate Activity. No exception is thrown. For the same, the folowing code works fine:
Intent intent = new Intent(this.context,Navigate.class);
intent.putExtra("params", params);
((Activity) context).startActivity(intent);
Please tell me if any work around.
I would use the more explicit constructor:
Intent (Context packageContext, Class<?> cls)
. From the docs:This constructor internally calls the
setComponent()
-method, which is also called by your usedsetClassName()
-method. But using the constructor spares you code-lines and makes the code more readable:Note that using
this
only works this way, when in anActivity
, because the activity-class extends theContext
-class.Another advantage over hard-coding the package and class-names as a String is, that when you move a class to another package (or rename a package), you'll get compile-time errors, not runtime-errors.
Also, for this to work, you'll need to register the Activity in the Android Manifest.
You need the full classname, including the package:
(And the Activity defined in the Manifest, but as you can start it with
Navigate.class
I assume you have already done this).