我正在使用Android生命周期,我发现一个似乎记录不佳的行为(或者我只是做错了)。
https://developer.android.com/reference/android/app/Activity.html#onCreateOptionsMenu(android.view.Menu)中指定的onCreateOptionsMenu方法应该只调用一次,但在方向更改期间打印两次。
以下是创建流日志,您可以看到在onPostResume方法之后立即调用onCreateOptionsMenu方法:
[...]
SimpleActivity >>> onStart
SimpleActivity <<< onStart
SimpleFragment >>> onStart
SimpleFragment <<< onStart
SimpleActivity >>> onPostCreate
SimpleActivity <<< onPostCreate
SimpleActivity >>> onResume
SimpleActivity <<< onResume
SimpleFragment >>> onResume
SimpleFragment <<< onResume
SimpleActivity >>> onPostResume
SimpleActivity <<< onPostResume
SimpleActivity >>> onCreateOptionsMenu
SimpleActivity <<< onCreateOptionsMenu
SimpleFragment >>> onCreateOptionsMenu
SimpleFragment <<< onCreateOptionsMenu
[...]
但是当由于方向改变而恢复状态时,也会在onRestoreInstanceState方法之后调用它:
[...]
SimpleActivity >>> onStart
SimpleActivity <<< onStart
SimpleFragment >>> onStart
SimpleFragment <<< onStart
SimpleActivity >>> onRestoreInstanceState
SimpleActivity >>> onCreateOptionsMenu
SimpleActivity <<< onCreateOptionsMenu
SimpleFragment >>> onCreateOptionsMenu
SimpleFragment <<< onCreateOptionsMenu
SimpleActivity >>> onPrepareOptionsMenu
[...]
SimpleActivity <<< onRestoreInstanceState
SimpleActivity >>> onPostCreate
SimpleActivity <<< onPostCreate
SimpleActivity >>> onResume
SimpleActivity <<< onResume
SimpleFragment >>> onResume
SimpleFragment <<< onResume
SimpleActivity >>> onPostResume
SimpleActivity <<< onPostResume
SimpleActivity >>> onCreateOptionsMenu
SimpleActivity <<< onCreateOptionsMenu
SimpleFragment >>> onCreateOptionsMenu
SimpleFragment <<< onCreateOptionsMenu
[...]
值得注意的是,此行为仅发生在API&gt; 19没有支持库。
用于记录的代码非常简单。 SimpleActivity配置内容视图并将一个SimpleFragment实例添加到FragmentManager(如果尚未添加):
public class SimpleActivity extends LoggerActivity {
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment);
Fragment f = getFragmentManager().findFragmentById(R.id.fragment_container);
findViewById(R.id.btn_dialog).setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
startActivity(new Intent(SimpleActivity.this, DialogActivity.class));
}
});
findViewById(R.id.btn_activity).setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
startActivityForResult(new Intent(SimpleActivity.this, ResultActivity.class), Utils.REQUEST_CODE);
}
});
if (f == null) {
getFragmentManager()
.beginTransaction()
.replace(R.id.fragment_container, new SimpleFragment())
.commit();
}
}
}
//////
public class SimpleFragment extends LoggerFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
setHasOptionsMenu(true);
super.onCreateView(inflater, container, savedInstanceState);
View v = inflater.inflate(R.layout.fragment_layout, container, false);
TextView txt = (TextView) v.findViewById(R.id.fragment_text);
txt.setText(this instanceof CustomName ? ((CustomName) this).getCustomName() : getClass().getSimpleName());
return v;
}
}
LoggerActivity和LoggerFragment实现所有生命周期方法,并在每个方法上调用Utils.logBeforeSuper和Utils.logAfterSuper。
public class LoggerActivity extends Activity {
/* [...] */
@Override
public boolean onCreateOptionsMenu(Menu menu) {
Utils.logBeforeSuper(this);
boolean b = super.onCreateOptionsMenu(menu);
Utils.logAfterSuper(this);
return b;
}
/* [...] */
}
public class LoggerFragment extends Fragment {
/* [...] */
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Utils.logBeforeSuper(this);
View v = super.onCreateView(inflater, container, savedInstanceState);
Utils.logAfterSuper(this);
return v;
}
/* [...] */
}
所以,我想知道预期的行为是什么?我错过了什么或没有记录吗?
一些链接
可以在此处找到完整的日志文件 (方向更改流程从第108行开始) :https://docs.google.com/spreadsheets/d/1u97W9QZIEDcJt1Q2xh5VQP4YliHjzHE0u2OlLF4y5WY/edit#gid=0
该项目托管在github上:(我没有足够的声誉来添加更多链接)
Whole Project: https://github.com/TartagliaEG/android-lifecycle-tests
LoggerActivity: /app/src/main/java/com/lifecycletests/base/loggers/LoggerActivity.java
LoggerFragment: /app/src/main/java/com/lifecycletests/base/loggers/LoggerFragment.java
Utils: /app/src/main/java/com/lifecycletests/utils/Utils.java
SimpleActivity: /app/src/main/java/com/lifecycletests/simple/SimpleActivity.java
SimpleFragment: /app/src/main/java/com/lifecycletests/simple/SimpleFragment.java