Reputation: 6068
I have posted this question before and the conclusion was that I needed more debug in order to understand the app's lifecycle. Here's the follow up, and I'm actually more confused.
My Application.onCreate method gets called twice: once when the app starts and once when I close it. Is this supposed to happen? The thing is, I have a service that I want to start when the app starts and stop when the app terminates. Apparently onCreate
is called in both circumstances? Please advise.
Note: I'm currently not interested in background execution.
Edit: When closing the app, Application.onCreate is called afer the main activity's Activity.onDestroy. I'm not sure how that could be relevant, and only adds to the confusion. The application is being "created" after its main activity is destroyed? Doesn't make sense.
Edit: Here to leave a sample log. The first time I ran the app and then closed it by tapping "Clear all". I believe that this would leave services running on the background. This log is for the second time I ran the app. Notice that onCreate
is not called when the app starts and that it's called instead after I close it:
06-22 19:54:29.971 2504-2504/com.demo.demochatdemo I/PhoneWindow: [generateLayout] setColorNavigationBar => color=0x ff000001 06-22 19:54:29.981 2504-2504/com.demo.demochatdemo D/PhoneWindowEx: [PWEx][generateLayout] setNavigationBarColor2 : colors=0xff000000 06-22 19:54:29.981 2504-2504/com.demo.demochatdemo I/PhoneWindow: [setNavigationBarColor2] color=0x ff000000 06-22 19:54:30.001 2504-2504/com.demo.demochatdemo I/Activity: Activity.onPostResume() called 06-22 19:54:30.011 2504-2504/com.demo.demochatdemo I/ViewRootImpl: CPU Rendering VSync enable = true 06-22 19:54:30.011 2504-3647/com.demo.demochatdemo D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true 06-22 19:54:30.021 2504-2504/com.demo.demochatdemo D/Atlas: Validating map... 06-22 19:54:30.051 2504-3647/com.demo.demochatdemo I/Adreno-EGL: : EGL 1.4 QUALCOMM build: (Iac7c2e2837) OpenGL ES Shader Compiler Version: E031.25.03.04 Build Date: 07/08/15 Wed Local Branch: LA_BR_1_2_3_RB1_AU080_1285665 Remote Branch: Local Patches: Reconstruct Branch: 06-22 19:54:30.051 2504-3647/com.demo.demochatdemo I/OpenGLRenderer: Initialized EGL, version 1.4 06-22 19:54:30.061 2504-3647/com.demo.demochatdemo D/OpenGLRenderer: Enabling debug mode 0 06-22 19:54:30.161 2504-2504/com.demo.demochatdemo V/ViewRootImpl: Contents drawing finished : com.demo.demochatdemo/com.demo.demochatdemo.ContactActivity 06-22 19:54:30.171 2504-2504/com.demo.demochatdemo W/IInputConnectionWrapper: showStatusIcon on inactive InputConnection 06-22 19:54:30.171 2504-2504/com.demo.demochatdemo I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@31f3e056 time:150361131
At this point I close the app and get the following:
06-22 19:54:40.621 2504-2504/com.demo.demochatdemo I/Activity: Activity.onPostResume() called 06-22 19:54:40.641 2504-2504/com.demo.demochatdemo I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@31f3e056 time:150371603 06-22 19:54:46.861 4061-4061/com.demo.demochatdemo I/com.demo.demochatdemo.ContactActivity: onCreate
onCreate
is being called there.
Edit: here's the manifest:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.demo.demochatdemo">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:name="com.demo.demochatdemo.ChatApplication">
<activity
android:name=".ContactActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ChatActivity">
</activity>
</application>
</manifest>
Edit: here's the code for the activity.
public class ContactActivity extends Activity implements Store.Delegate {
private static final String TAG = ContactActivity.class.getName();
private String displayName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ListView listView;
final ChatApplication chatApplication = (ChatApplication)getApplication();
final ContactActivity contactActivity = this;
setContentView(R.layout.contact_view);
listView = (ListView) findViewById(R.id.contact_view);
listView.setAdapter(new ContactViewAdapter(this, chatApplication.getStores(), new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if (MotionEvent.ACTION_UP == motionEvent.getAction()) {
Intent intent = new Intent(ContactActivity.this, ChatActivity.class);
TextView displayName = (TextView) view.findViewById(R.id.display_name);
CharSequence charSequence = displayName.getText();
setDisplayName(charSequence.toString());
intent.putExtra(ChatActivity.INTENT_EXTRA_STORE, charSequence);
startActivity(intent);
}
return true;
}
}));
}
}
Upvotes: 0
Views: 800
Reputation: 22040
Do you stop the service before you close the app? The default implementation of Service.onStartCommand() returns START_STICKY. If you're not overriding it, I believe the service will be restarted which, implicitly, forces an Application instance to be started.
Upvotes: 1