Android应用程序VS活动 [英] Android Application vs Activity

查看:98
本文介绍了Android应用程序VS活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写了一些Android应用程序,并一直宣称起始活动为:

I have written a few Android apps, and have always declared a starting Activity as the:

<intent-filter>
    <action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

这将是伟大的范围界定一些全球性的方法,静态,共享preFS等,如果我可以使用应用程序启动我的应用程序的然后调用第一个活动从它的的onCreate()设置preFS等,但我一直没能找到后这种设计模式中的任何例子...当我尝试在code,我收到了 ClassCastException异常

It would be great for scoping some global methods, statics, shared prefs, etc if I could start my app using an Application that then calls the first Activity from it's onCreate() after setting up prefs, etc, but I haven't been able to find any examples of this design pattern... when I try this in code, I get a ClassCastException:

public class MyApplication extends Application {
@Override
    public void onCreate() {
        super.onCreate();

        // do stuff (prefs, etc)

        // start the initial Activity
        Intent i = new Intent(this, InitialActivity.class);
    startActivity(i);
    }
}

InitialActivity.class 的确是一个活动工作正常,如果我将其设置为,而是试图从所有MyApplication 的声明生成错误。可能是一个非常愚蠢的问题,但我会处理这一切都错了吗?

InitialActivity.class is indeed an Activity that works fine if I set it to be MAIN, but trying to start it from MyApplication that is declared MAIN generates the error. Probably a very silly question, but am I tackling this all wrong?

谢谢

推荐答案

您可以使用 FLAG_ACTIVITY_NEW_TASK 标记解决这个问题:

You can fix this by using FLAG_ACTIVITY_NEW_TASK flag:

Intent intent = new Intent(this, ApplicationActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

这是因为你需要开始时,活动的活动环境之外开始新的任务。但我强烈建议不要从开始的活动应用程序的的onCreate()

That's because you need to start new task when Activity is started outside of Activity context. But I strongly recommend to not start Activity from your Application's onCreate().

Android有4个组成部分:活动,服务,ContentProvider的和广播

Android has 4 components: Activity, Service, ContentProvider and Broadcast.

当机器人需要从你的应用程序激活此组件之一,它看起来如果有已经存在与应用程序运行的进程。如果没有,那么Android的启动新的进程,初始化它,然后它初始化您的自定义应用程序实例。然后它激活所需的组件之一。

When Android needs to activate one of this components from your application, it looks if there is already existing running process with your application. If not, then Android starts new process, initializes it, then it initializes your custom Application instance. And then it activates one of needed components.

现在,让我们考虑下一个场景:你的应用程序中声明在的Andr​​oidManifest.xml 内容提供商,而Android只是来启动应用程序,这样可以提供一些数据到另一个前景应用程序。

Now, let's consider next scenario: your application declared content provider in AndroidManifest.xml, and Android just about to start your application so you can provide some data to another foreground application.

  1. 在内容提供者发送请求
  2. 您的应用程序没有运行,而Android开始新的进程吧。
  3. 在创建自定义应用程序实例
  4. Application.onCreate()之称。
  5. 您开始活动
  6. 您内容提供商接收请求
  1. Content Provider request is sent
  2. Your application wasn't running, and Android starts new process for it.
  3. Your custom Application instance is created
  4. Application.onCreate() is called.
  5. You start an activity
  6. Your Content Provider receives request

有人只是想连接到您的内容提供商,但你的应用程序启动的活动来代替。同一真正启动后台服务,有时广播接收机。

Somebody just wanted to connect to your content provider, but your application started an Activity instead. Same true for starting background Service and sometimes broadcast receivers.

和还认为,如果其他应用程序的活动一想开始活动x在您的应用程序。但在的onCreate()您开始活动Y和那么X也通过Android的开始。然后,用户presses回来。会发生什么?它的棘手......

And also consider if some other application's activity A wanted to started activity X from your application. But in onCreate() you started activity Y, and then X is also started by Android. Then user presses back. What should happen? Its tricky...

开始从申请活动的onCreate 可能会导致非常奇怪的用户体验。所以,不要做。

Starting activities from Application's onCreate may result in quite weird user experience. So don't do it.

更新: 由于Android可以保证应用程序将创建只有一次,任何其他组件之前,你可以使用下一个code来访问应用程序的单个实例:

UPDATE: Because Android guarantees that Application will be created only once and before any other component, you can use next code to access your Application's single instance:

public class MyApplication extends Application 
{   
    private static MyApplication s_instance;

    public MyApplication()
    {
        s_instance = this;
    }

    public static MyApplication getApplication()
    {
        return s_instance;
    }
}

这篇关于Android应用程序VS活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆