Android Application->onCreate() not called when reopen app
Asked Answered
E

2

6
public class MyApplication extends Application{

@Override
public void onCreate()
{
    super.onCreate();
    Log.d("*******:", "onCreate");
}}

public class MainActivity extends ActionBarActivity{

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}}

<application
    android:name=".MyApplication"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

When this Application is opened first time, the onCreate() method in MyApplication has been called,but when I finish the Activity(Press the Back Button), then reopen the app,I found the onCreate() method in MyApplication NOT been called.

And it is weird that if I "kill" the app in system background,then reopen it, I found the onCreate() method could be called again.

I don't know why,and I want to get the action when user reopen my application,could anyone help? Thanks!

Epimenides answered 2/7, 2015 at 10:25 Comment(5)
I think you need to add log report, so we can better understand your problem.Belda
@Belda It is a quite simple demo, I think the log report maybe not necessary.My description may not very clear,I will edit my question description.Thanks anyway.Epimenides
you need to understand activity life cycle ...Paunch
Maybe, not sure, this will inform you a bit: developer.android.com/reference/android/app/Application.htmlBlissful
@MahaKhan I know Activity life cycle,I am not asking for that,but Application's life cycle.Epimenides
M
5

Your MyApplication is extending Application. So it does make sense that onCreate is not called when reopening the app as the application is in memory

If it was extending Activity the onCreate will be called when reopening

Murray answered 2/7, 2015 at 10:48 Comment(0)
P
3

OnCreate is only for initial creation, and thus should only be called once. onCreate() method performs basic application startup logic that should happen only once for the entire life of the activity .

The initialization process takes lot of resources and to avoid this the activity once created is never completely destroyed but remains non visible to user in background so that once it is bring back to front , reinitialization doesn't happen .

Once the onCreate() finishes execution, the system calls the onStart() and onResume() methods in quick succession.

If you have any processing you wish to complete multiple times you should put it elsewhere,like in the @OnResume method.

protected void onRestart() {
  Log.d("*******:", "onRestart");
    super.onRestart();

}

protected void onResume() {
    super.onResume();
  Log.d("*******:", "OnREsume");
}

Hope You Understand !!

Paunch answered 2/7, 2015 at 10:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.