I have a little problem. I want to start activity but in something other way. I know that
Intent i = new Intent(this, ActivityTwo.class);
initialize intent and after that I can startActivity
. But I want do something like that:
Intent i = new Intent(this, MyString.class);
I have no nameActivity.class
, but I want change on string.class. How I can start activity when I have string name of class?
1.2. To start an activity, use the method startActivity(intent) . This method is defined on the Context object which Activity extends. The following code demonstrates how you can start another activity via an intent. # Start the activity connect to the # specified class Intent i = new Intent(this, ActivityTwo.
First you use startActivityForResult() with parameters in the first Activity and if you want to send data from the second Activity to first Activity then pass the value using Intent with the setResult() method and get that data inside the onActivityResult() method in the first Activity .
Try this:
startActivity(this, Class.forName(yourStringClass));
Here is a code by which you can start activity using the name of the activity
Class<?> c = null;
if(StringClassname != null) {
try {
c = Class.forName(StringClassname );
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Intent intent = new Intent(mail.this, c);
startActivity(intent);
Here class name will be full name of the class with the package name. For example if your package name will be x.y.z and if you have Activity name called A then the full name of the Activity A will be x.y.z.A.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With