我正在玩 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
[...]
值得注意的是,这种行为只发生在没有 Support 库的 API > 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