简体   繁体   中英

Android: Using an intent to start any activity

I have been working on an application that has a bunch of formulas in it. The user can select which formula they need to use, input the numbers/variables, and the program will return whatever answer they are looking for. Each formula has its own class and since there are so many classes I sorted them into separate packages.

For Example , I might have 3 packages ( Volume , Area , and Main ).

  • In the Volume package, I might have 2 classes ( Cube_Volume , and Sphere_Volume ).
  • In the Area package, I might have 3 classes ( Square_Area , Circle_Area , and Triangle_Area ).
  • In the Main package, I might have 1 class ( Main_Activity ).

I am currently trying to allow the user to bookmark their favorite formulas. If a user is in the Sphere_Volume class, they can bookmark that class. How I am currently attempting to do this is when the user chooses to bookmark the class, the application saves the string returned by this code.

this.getLocalClassName()

The string that is saved (I am using an SQL database to store the string) will look something like this:

com.example.area.Triangle_Area

The bookmarks that the user saves (like the one shown above) are displayed in a Listview in the Main_Activity class. Obviously, when the user clicks on a bookmark in the Listview , I would like the program to start the corresponding activity.

So finally, the question is: How can I use an Intent to start any activity in any other package when the string provided is something like com.example.area.Triangle_Area ?

Thanks for any and all answers! Please comment if I need to elaborate more on my issue.

由于Intent构造函数接受组件类作为其参数,因此就这么简单:

context.startActivity(new Intent(context, Class.forName("com.example.area.Triangle_Area"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM