class - 安卓状态机

标签 class android-intent android-activity android

我编写了一个 Android 应用程序。我的应用程序中有很多 Activity , Activity 之间有大量转换。我决定实现一个状态机,它将切换我的 Activity 。

我希望在我的 Application 类或其子类中实现一个状态机,但我无法从中启动任何 Activity。

public class MainAppClass extends Application {}

这样的代码是失败的:

Intent intent = new Intent();
intent.setClass(this.MyActivity, Screen2Activity.class);
StartActivity(intent);

也许,这是不真实的?这些任务还有其他方法吗? 感谢您的回答!

最佳答案

这是另一个问题:您为什么要尝试从 Application 的子类启动 Activity ?

阅读本文(来自文档),您会明白为什么您尝试做的事情没有任何意义。

The Application class is the base class for those who need to maintain global application state. There is normally no need to subclass Application. In most situation, static singletons can provide the same functionality in a more modular way. If your singleton needs a global context (for example to register broadcast receivers), the function to retrieve it can be given a Context which internally uses Context.getApplicationContext() when first constructing the singleton.

如果您编辑您的原始帖子,阐明您尝试这样做的原因,也许我可以为您指出一种更合适的方式来启动您的 Screen2Activity

关于class - 安卓状态机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10933930/

相关文章:

java - 我在导入最近制作的类(class)时遇到问题

Objective-C 重新定义类

android - 共享意向位置

java - 从 parse.com 检索字符串并在 Android TextView 中显示它

android - 从 Activity 类中重构 performCrop() 方法

android - 创建 BroadcastReceiver 的最佳方式是什么?

class - 在 VBA 中使用自定义数据类型

java - 如何解决 ClassNotFoundException?

java - 保存图像后检查 SD 卡 (Android)

java - Android:如何将回调传递给 Activity ?