简体   繁体   English

Android:使用意图开始任何活动

[英]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 ). 例如 ,我可能有3个包VolumeAreaMain )。

  • In the Volume package, I might have 2 classes ( Cube_Volume , and Sphere_Volume ). Volume包中,我可能有2个类Cube_VolumeSphere_Volume )。
  • In the Area package, I might have 3 classes ( Square_Area , Circle_Area , and Triangle_Area ). Area包中,我可能有3个类Square_AreaCircle_AreaTriangle_Area )。
  • In the Main package, I might have 1 class ( Main_Activity ). Main包中,我可能有1个类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. 如果用户在Sphere_Volume类中,则他们可以为该类添加书签。 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: 保存的字符串(我正在使用SQL数据库存储该字符串)将如下所示:

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. 用户保存的书签(如上面显示的书签)显示在Main_Activity类的Listview中。 Obviously, when the user clicks on a bookmark in the Listview , I would like the program to start the corresponding activity. 显然,当用户单击Listview中的书签时,我希望程序启动相应的活动。

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 ? 所以最后,问题是:当提供的字符串类似于com.example.area.Triangle_Area时,如何使用Intent来启动任何其他包中的任何活动?

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"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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