Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
284 views
in Technique[技术] by (71.8m points)

android - What methods are invoked in the Activity Lifecycle in the following cases:

Let's say I have a Hello World single Activity application. I start this application.

What methods are invoked in each case:

  • Home button is pressed: ?
    Back button is pressed: ?
    Phone call is received: ?

What methods are invoked once the user starts the application again via the app icon (assuming the OS hasn't had a "other apps need memory condition"):

  • Home button was pressed: ?
    Back button was pressed: ?
    Phone call was received: ?

Thanks all.

Edit: Extra Credit: How can the user invoke onPause without invoking onStop?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

For understand ACTIVITY LIFECYCLE i create the demo See HERE

And different case study i added.

MainActivity.java

    public class MainActivity extends AppCompatActivity {

    private static final String TAG = "State changed";

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

        Log.i(TAG, "onCreate: ");
    }

    public void OpenDialog(View view) {

        final AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create(); //Read Update
        alertDialog.setTitle("hi");
        alertDialog.setMessage("this is my app");

        alertDialog.setButton("Continue..", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                // here you can add functions
                alertDialog.dismiss();
            }
        });

        alertDialog.show();  //<-- Show dialog
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.i(TAG, "onStart: " );
    }

    @Override
    protected void onResume() {
        super.onResume();

        Log.i(TAG, "onResume: ");

    }

    @Override
    protected void onPause() {
        super.onPause();

        Log.i(TAG, "onPause: ");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.i(TAG, "onStop: ");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.i(TAG, "onRestart: ");
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        Log.i(TAG, "onSaveInstanceState: ");
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        Log.i(TAG, "onRestoreInstanceState: ");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.i(TAG, "onDestroy: ");
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        Log.i(TAG, "onBackPressed: ");
    }

}

Case Study

Case 1 = First click the app icon

Note : here does not show the onRestoreInstanceState because it execute on runtime

I/State changed: onCreate:

I/State changed: onStart:

I/State changed: onResume:

Case 2 = Click Home Button (same happended when screen light off or coming call)

I/State changed: onPause:

I/State changed: onSaveInstanceState:

I/State changed: onStop:

Case 3 = (case 2 continue) Open App via Recent

I/State changed: onRestart:

I/State changed: onStart:

I/State changed: onResume:

Case 4 = Click Back button (onBackPressed method call)

I/State changed: onPause:

I/State changed: onStop:

I/State changed: onDestroy:

Case 5 = Configration Change (Rotate the screen)

I/State changed: onPause:

I/State changed: onSaveInstanceState:

I/State changed: onStop:

I/State changed: onDestroy:

I/State changed: onCreate:

I/State changed: onStart:

I/State changed: onRestoreInstanceState:

I/State changed: onResume:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...