我编写了一个 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 aContext
which internally usesContext.getApplicationContext()
when first constructing the singleton.
如果您编辑您的原始帖子,阐明您尝试这样做的原因,也许我可以为您指出一种更合适的方式来启动您的 Screen2Activity
。
关于class - 安卓状态机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10933930/