问题描述
有关活动的官方文档列出了7种生命周期方法.
Official documentation about Activity lists out 7 life cycle methods.
onPostResume()
未引用为生命周期方法.
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()
已被依次调用.
我的代码段:
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);
}
}
跳过以下方法来实现onPostResume()
不能达到目的吗?
Implementing onPostResume()
by skipping below methods doesn't serve the purpose?
onRestart(), onStart(), onResume()
如果实现onPostResume()
,实现这三种方法有什么优势?
What are the advantages of implementing these three methods if I implement onPostResume()
?
onRestart(), onStart(), onResume()
推荐答案
onPostResume:
在活动恢复完成时调用(在活动的{@link #onResume}之后 被称为).应用程序通常不会实现此方法; 它旨在让系统类在应用后进行最终设置 恢复代码已运行.
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 ensure that screen is visible to user and will do the finalset up for activity.
删除其中包含代码为"what"的所有待处理邮件 这 消息队列.
Remove any pending posts of messages with code 'what' that are in the message queue.
检查所有片段是否恢复并移动由...管理的所有片段 控制器的FragmentManager进入恢复状态.
Check all fragment gets resumed and Moves all Fragments managed by the controller's FragmentManager into the resume state.
对由管理的片段执行的所有未决操作 控制器的FragmentManager.
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()-活动
onResume() - Activity
onResume()-片段check third point as explained above
onResume() - Fragment check third point as explained above
onPostResume()-活动
onPostResume() - Activity
这篇关于活动生命周期方法:onPostResume的意义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!