0

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();
  • You might want to use intent? https://stackoverflow.com/questions/3872063/how-to-launch-an-activity-from-another-application-in-android – Bryan.C Jan 14 '22 at 20:47
  • I have edited the code, yes i am using intent. Intent intent = new Intent(otherContext, otherClass); startActivity(intent); – Marius Nicolae Jan 14 '22 at 20:59

0 Answers0