多个 Activity 声明可以共享相同的类名吗?
例如
<manifest>
<activity android:label="MyActivity1" android:name=".MyClass">
</activity>
<activity android:label="MyActivity2" android:name=".MyClass">
</activity>
</manifest>
For example
<manifest>
<activity android:label="MyActivity1" android:name=".MyClass">
</activity>
<activity android:label="MyActivity2" android:name=".MyClass">
</activity>
</manifest>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是和不是。当您在
Activity
名称前加上.
时,它会查看清单的默认包以获取整个类路径,例如com.example.android.MyClass
。因此,如果您有一个.MyClass
和另一个com.example.android.other.MyClass
,那么这应该可以工作。Yes and no. When you prepend the
Activity
name with a.
, it looks at the Manifest's default package to get the whole class path, such ascom.example.android.MyClass
. Thus, if instead you have one.MyClass
and anothercom.example.android.other.MyClass
, then this should work.我不确定这是否可能,但也许有更好的方法来解决这个问题。如果您需要相同的活动,您可以像平常一样在两种情况下调用它,但也在调用期间传递数据。在 MyClass 中,您可以读取数据并决定如何处理它。
例子:
//Activity 1
//Activity 2
在 MyActivity 中的 onCreate() 中执行类似的操作
这是一个非常粗略的示例,您可以使用整数并打开这些值等。但真正的目标是让一个 Activity 处理多种情况。这似乎是你想要的,因为无论如何你都会使用同一个类。
I'm not 100% sure if this is possible, but perhaps there is a better way to go about this. If you need the same activity you can call it in both situations like you normally would but pass in data during the call as well. In your MyClass you can read the data and decide how to handle it.
Example:
//Activity 1
//Activity 2
And in MyActivity do something like this in onCreate()
This is a pretty rough example, you could use ints and switch on those etc. But the real goal is to let one activity handle a variety of cases. This seems to be what you want since your going to use the same class anyway.