开发者

Start an activity from code

I have a package name and an activity name and开发者_如何学C I want to start a new class or activity using it. Is it possible to start the activity from code without declaring the same activity in the manifest file?


You can't start an activity if it isn't declared in the Manifest. All you might do is change the views programaticaly, but not the activity.


No, you can't do this (see the 2nd paragraph of the Activity 'Class Overview'). However...

  1. If you want to do this to prevent people launching sub-activities directly, you can put logic in to detect the launching activity (via Activity.getCallingActivity / Activity.getCallingPackage) - you'll need to use StartActivityForResult to do this

  2. You might be able to achieve what you want through a ClassLoader - more details on what you're doing (and why) would be useful.

  3. If you're trying to launch an activity defined in a different package, then it only needs to be declared in the other package's manifest - you can then specify it with Intent.setClassName

Hope this helps,

Phil Lello

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜