In Android Studio, i am reading the book: "Android Development for Dummies(2nd edition)" I copied the exact code from the book so I'm trying to start another application activity, here is the code I'm using:
Context otherContext;
String otherContextString = "com.taozen.weather_sunshine";
try{
otherContext = otherContext.createPackageContext(otherContextString, Context.CONTEXT_IGNORE_SECURITY | Context.CONTEXT_INCLUDE_CODE);
}
catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
Class<?> otherClass = null;
try{
otherClass = otherContext.getClassLoader().loadClass("com.taozen.weather_sunshine.MainActivity");
}
catch (ClassNotFoundException e){
e.printStackTrace();
}
Intent intent = new Intent(otherContext, otherClass);
startActivity(intent);
It works like this, but i wanna know why the method above doesn't work.
// Intent intent = new Intent(Intent.ACTION_MAIN);
// intent.setClassName("com.taozen.weather_sunshine", "com.taozen.weather_sunshine.MainActivity");
// startActivity(intent);
// finish();