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()
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
It will ensure that screen is visible to user and will do the final set up for activity.
Remove any pending posts of messages with code 'what' that are in the message queue.
Check all fragment gets resumed and Moves all Fragments managed by the controller's FragmentManager into the resume state.
Execute any pending actions for the Fragments managed by the controller's FragmentManager.
If you check it life cycle vise it worked like below
onResume() - Activity
onResume() - Fragment check third point as explained above
onPostResume() - Activity
onPostResume
is mainly reserved for system actions which want to finish some kind of setup after any subclasses have finished resuming.
The two things it is good for, (which might make you feel it's important) are for doing actions after your nested fragments are also resumed and when the applications is guaranteed to be visible to the user (it may not yet be visible during onResume).
It can be a little confusing from the method names when looking at the Sources, but if you log the flow you'll see what happens is
I will add a word of caution for any future readers - if you use onPostResume to wait for your fragment, to call some get method (for example), then you have a bad, unreliable design. You should instead have a callback pattern from your fragment and have the fragment "send" the data back to the activity when it is ready
Before answering your question lets talk about onPostResume()
According to android
onPostResume()
added in API level 1
void onPostResume ()
Called when activity
resume is complete (after 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.
So as they said it is called once the activity is resumed. So if you want to do something after an activity is resumed you can use this method. But mostly we do all the stuff in onResume()
like begin animations, open exclusive-access devices (such as the camera), etc. so once Activity is resumed onPostResumed()
get called. So don't you think it differs from on resume? Because in onPostResume() according to os activity is already resumed.And for the onPause and onStop().They are different. They are when an activity is going into the background but has not (yet) been killed and Called when you are no longer visible to the user respectively. So they are different from onResume()
and onPostResume()
.
Now before relying on onPostResume() you should read these two things
As far as developers.android
1 - Applications will generally not implement this method; it is intended for system classes to do final setup after application resume code has run.
(But we can use it for our purpose no matter what the intent of this).
2 - Check this link . He said that onPostResume() is not ALWAYS called . And his case was for fragments ( Of course they are over an activity man ).
Here is the reply,
The issue is completely resolved in API level 16 and support library rev 9.
New method "FragmentActivity.onResumeFragments()
" should be used.
So some of them having issues in relying on this method so it's up to you know.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With