Wednesday, August 10, 2011

Opening an Activity with string in Android

This question has been asked here http://stackoverflow.com/questions/2387139 and http://stackoverflow.com/questions/2184226/how-to-use-variable-while-calling-new-activity-in-intent and my problem is similar but i cant see the destination classes coz the classes are from different libraries and therefore i need to open an activity based on just the string and here is how i fixed mine:



view plaincopy to clipboardprint?



try {

String className = 'com.almondmendoza.library.openActivity';

Intent openNewIntent = new Intent( this, Class.forName( className ) );

startActivity( openNewIntent );

} catch (ClassNotFoundException e) {

e.printStackTrace();

}





Explanation

Here we use reflections and just convert our class name to a class object.



If you are interested in reflection we had blogged about it previously Access internal classes in Android and Calling Private Methods in Android

No comments:

Post a Comment