什么时候申请的onCreate()方法被调用在Android?什么时候、方法、onCreate、Android

2023-09-12 04:27:15 作者:被驯服的猛虎

在我的Andr​​oid应用我有延伸android.app.Application和DefaultApplication中onCreate方法我绑定了一些服务,在此应用程序将使用我的其他活动DefaultApplication中类。另外我有一个BroadcastReceiver监听并接收C2DM消息。当这个接收器接收,当应用程序没有运行的消息,它会触发一个对话框,显示即将到来的消息,它将开始我的应用程序的活动。

我的问题是,当我开始不与DefaultApplication中任何互动的活动,将我的DefaultApplication中的onCreate方法被调用,因为该应用程序的活动开始?

下面是定义和我的DefaultApplication中的清单:

 公共类DefaultApplication中扩展应用{

    @覆盖
    公共无效的onCreate(){
        super.onCreate();

        doBindService();

    }

    无效doBindService(){

        //建立与服务的连接。我们使用一个明确的
        //类的名字,因为我们希望有一个具体的服务实现
        //我们知道将在我们自己的进程中运行(并且因此将不会被
        //配套部件更换其他应用程序)。

        bindService(新意图(DefaultApplication.this,SocketService.class)
                socketServiceConnection,Context.BIND_AUTO_CREATE);

        mIsBound = TRUE;
    }

    无效doUnbindService(){
        如果(mIsBound){
            //分离我们的现有连接。
            unbindService(socketServiceConnection);
            mIsBound = FALSE;
        }
    }
}
 
Android开发 在onCreate方法中两次调用setContentView

清单如下:

 <应用机器人:图标=@可绘制/图标机器人:标签=@字符串/ APP_NAME
        机器人:名称=com.mypackage.DefaultApplication
        机器人:主题=@安卓风格/ Theme.NoTitleBar.Fullscreen
        机器人:可调试=真正的>
<服务机器人:名称=com.mypackage.services.SocketService>< /服务>
<活动机器人:名称=TestActivity
            机器人:screenOrientation =山水和GT;< /活性GT;
< /用途>
 

解决方案

仅在第一次。

在活动开始和应用未加载,那么这两个的onCreate()的方法将被调用。

但对于后续活动的开始,在的onCreate()申请将不会被调用。

In my Android application I have a DefaultApplication class which extends android.app.Application and in DefaultApplication onCreate method I bind some services which will be used by my other Activities in this app. Also I have a BroadcastReceiver which listens and receives C2DM Messages. When this receiver receives a message when the application is not running, it will fire a dialog which shows the upcoming message and it will start an Activity of my application.

My question is, when I start an activity without any interaction with DefaultApplication, will my DefaultApplication's onCreate method get called because an Activity of that application is started?

Here are the definition and Manifest of my DefaultApplication:

public class DefaultApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();

        doBindService();

    }

    void doBindService() {

        // Establish a connection with the service. We use an explicit
        // class name because we want a specific service implementation that
        // we know will be running in our own process (and thus won't be
        // supporting component replacement by other applications).

        bindService(new Intent(DefaultApplication.this, SocketService.class),
                socketServiceConnection, Context.BIND_AUTO_CREATE);

        mIsBound = true;
    }

    void doUnbindService() {
        if (mIsBound) {
            // Detach our existing connection.
            unbindService(socketServiceConnection);
            mIsBound = false;
        }
    }
}

Manifest looks like this:

<application android:icon="@drawable/icon" android:label="@string/app_name"
        android:name="com.mypackage.DefaultApplication"
        android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
        android:debuggable="true">
<service android:name="com.mypackage.services.SocketService"></service>
<activity android:name="TestActivity"
            android:screenOrientation="landscape"></activity>
</application>

解决方案

Only the first time.

When Activity is started and application is not loaded, then both onCreate() methods will be called.

But for subsequent starts of Activity, the onCreate() of application will not be called.