0

I have a very simple application with 2 activities.

MainActivity

public class MainActivity extends AppCompatActivity {
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btnToTwo = findViewById(R.id.btnToTwo);
        btnToTwo.setOnClickListener(v -> {
            Intent i = new Intent(this, SecondActivity.class);
            startActivity(i);
        });

        Log.d("status", "1 onCreate");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d("status", "1 onDestroy");
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.d("status", "1 onStart");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.d("status", "1 onStop");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d("status", "1 onPause");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d("status", "1 onResume");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.d("status", "1 onRestart");
    }

SecondActivity

public class SecondActivity extends AppCompatActivity {

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

        Button btnToOne = findViewById(R.id.btnToOne);
        btnToOne.setOnClickListener(v -> {
            Intent i = new Intent(this, MainActivity.class);
            startActivity(i);
        });

        Log.d("status", "2 onCreate");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d("status", "2 onDestroy");
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.d("status", "2 onStart");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.d("status", "2 onStop");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d("status", "2 onPause");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d("status", "2 onResume");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.d("status", "2 onRestart");
    }
}

Once the application gets launched, I navigate to the second activity and return to the main activity. The logcat results are given as follows.

Launching the app:

1 onCreate
1 onStart
1 onResume

Navigating to the second activity:

1 onPause
2 onCreate
2 onStart
2 onResume
1 onStop

Returning to the first activity:

2 onPause
1 onCreate  <------------ Why do we need to recreate the main activity?
1 onStart
1 onResume
2 onStop

Question

Why is there an onCreate call on MainActivity (when returning to MainActivity from SecondActivity) while there is no onDestroy() called on the MainActivity?

  • When moving between activities, the former activity gets removed (aka destroyed) so when you navigate back to it, it is created again. Regarding onDestroy, refer to [this](https://stackoverflow.com/questions/18361719/android-activity-ondestroy-is-not-always-called-and-if-called-only-part-of-the). – tomerpacific Aug 02 '21 at 06:20

0 Answers0