Android app & # 8594; onCreate () not called when reopening the app

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 app opens for the first time, the onCreate () method in MyApplication was called, but when I complete the action (press the back button), then reopen the app, I found the onCreate () method in MyApplication NOT was called.

And it's strange that if I "kill" the application in the system background and then open it again, I found that the onCreate () method could be called again.

I don't know why and I want to get the action when the user reopens my application, can anyone help? Thank!

+3


source to share


2 answers


Yours is MyApplication

spreading Application

. So it makes sense that onCreate

it is not called when the app is reopened as the app is in memory



If it expanded Activity

, it onCreate

will be called when reopened

+5


source


OnCreate is for initial creation only and therefore only needs to be called . The onCreate () method executes basic application startup logic, which should only happen once for its entire lifecycle.

The initialization process is resource intensive, and to avoid this, the once created activity is never completely destroyed, but remains invisible to the user in the background, so no reinitialization is performed when it returns to the foreground .

After the execution of onCreate () is complete, the system quickly calls the onStart () and onResume () methods.



If you have processing that you want to perform multiple times, you must put it somewhere else, such as the @OnResume method.

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

}

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

      

Hope you understand !!

+3


source







All Articles