Activity life cycle methods : onPostResume significance

Ravindra babu picture Ravindra babu · Feb 7, 2017 · Viewed 17.3k times · Source

Official documentation about Activity lists out 7 life cycle methods.

onPostResume() was not quoted as life cycle method.

But I feel that this method is important method.

During the life cycle, when an activity is visible from hidden to show state,

onRestart()
onStart()
onResume()
onPostResume()

have been invoked in order.

My code snippet:

package ravindra.projects.my_app_1;

import android.content.Intent;
import android.content.IntentFilter;
import android.os.PersistableBundle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import android.widget.TextView;


public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private EditText txtUserName;
    private EditText txtPassword;
    Button  loginButton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d("Ravi","Main OnCreate");
        txtUserName=(EditText) findViewById(R.id.username);
        txtPassword=(EditText) findViewById(R.id.password);
        loginButton =  (Button)  findViewById(R.id.login);
        loginButton.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        Log.d("Ravi", "Login processing initiated");
        Intent intent = new Intent(this,LoginActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString("userName",txtUserName.getText().toString());
        bundle.putString("password",txtPassword.getText().toString());
        intent.putExtras(bundle);
        startActivityForResult(intent,1);
       // IntentFilter
    }
    public void onActivityResult(int requestCode, int resultCode, Intent resIntent){
        Log.d("Ravi back result:", "start");
        String result = resIntent.getStringExtra("result");
        Log.d("Ravi back result:", result);
        TextView txtView = (TextView)findViewById(R.id.txtView);
        txtView.setText(result);
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.d("Ravi","Main Start");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.d("Ravi","Main ReStart");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d("Ravi","Main Pause");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d("Ravi","Main Resume");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.d("Ravi","Main Stop");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d("Ravi","Main OnDestroy");
    }

    @Override
    protected void onPostResume() {
        super.onPostResume();
        Log.d("Ravi","Main PostResume");
    }

    @Override
    public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
        super.onSaveInstanceState(outState, outPersistentState);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
    }
}

Implementing onPostResume() by skipping below methods doesn't serve the purpose?

onRestart(), onStart(), onResume()

What are the advantages of implementing these three methods if I implement onPostResume() ?

onRestart(), onStart(), onResume()

Answer

jitsm555 picture jitsm555 · Mar 20, 2017

onPostResume :

Called when activity resume is complete (after activity's {@link #onResume} has been called). Applications will generally not implement this method; it is intended for system classes to do final setup after application resume code has run.

It will do following things

  1. It will ensure that screen is visible to user and will do the final set up for activity.

  2. Remove any pending posts of messages with code 'what' that are in the message queue.

  3. Check all fragment gets resumed and Moves all Fragments managed by the controller's FragmentManager into the resume state.

  4. Execute any pending actions for the Fragments managed by the controller's FragmentManager.

If you check it life cycle vise it worked like below

  1. onResume() - Activity

  2. onResume() - Fragment check third point as explained above

  3. onPostResume() - Activity