我在调试涉及方向更改和一些空返回的情况时遇到困难。
我的问题分为:
1)当方向改变时,会执行Activity的生命周期回调。
2)当方向改变时,会执行Fragment生命周期的哪些回调。
3)如果我们合并第2点和第3点(通常是覆盖Activity的片段的情况),对于具有片段的Activity(执行流程?),回调执行流程是什么。
修改
如果在android清单文件中没有定义configChanges,那么就可以解决这个问题:
如果发生任何未选择由该属性报告的配置更改,则系统将停止并重新启动活动(而不是使用新配置启动),而不是报告它。
活动和片段的生命周期回调将被执行?
答案 0 :(得分:6)
首先,在 AndroidManifest 中将其添加到您的活动中:android:configChanges="orientation"
1)。 onConfigurationChanged
2)。没什么,但你可以实施你的方法并通过onConfigurationChanged的活动来调用它
3)。流程为Activity.onConfigurationChanged -> Fragment.yourMethod
希望这有帮助。
<强>已更新强>
以下是您第一次开始活动时的流程:
Activity.onCreate
Activity.onStart
Fragment.onAttach
Fragment.onCreate
Fragment.onCreateView
Fragment.onViewStateRestored
Fragment.onStart
Activity.onResume
Fragment.onResume
以下是改变方向后的流程:
Activity.onPause
Fragment.onPause
Activity.onSaveInstanceState
Fragment.onSaveInstanceState
Activity.onStop
Fragment.onStop
Activity.onDestroy
Fragment.onDestroy
Fragment.onDetach
Fragment.onAttach
Fragment.onCreate
Activity.onCreate
Activity.onStart
Fragment.onCreateView
Fragment.onViewStateRestored
Fragment.onStart
Activity.onRestoreInstanceState
Activity.onResume
Fragment.onResume
以下是检查它的代码:
public class FooActivity extends FragmentActivity {
private static final String LOG_TAG = FooActivity.class.getSimpleName() + "_TAG";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i(LOG_TAG, "Activity.onCreate");
setContentView(R.layout.activity_foo);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new BarFragment())
.commit();
}
}
@Override
protected void onPause() {
Log.i(LOG_TAG, "Activity.onPause");
super.onPause();
}
@Override
protected void onStop() {
Log.i(LOG_TAG, "Activity.onStop");
super.onStop();
}
@Override
protected void onDestroy() {
Log.i(LOG_TAG, "Activity.onDestroy");
super.onDestroy();
}
@Override
protected void onResume() {
Log.i(LOG_TAG, "Activity.onResume");
super.onResume();
}
@Override
protected void onStart() {
Log.i(LOG_TAG, "Activity.onStart");
super.onStart();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
Log.i(LOG_TAG, "Activity.onConfigurationChanged");
super.onConfigurationChanged(newConfig);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
Log.i(LOG_TAG, "Activity.onSaveInstanceState");
super.onSaveInstanceState(outState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
Log.i(LOG_TAG, "Activity.onRestoreInstanceState");
super.onRestoreInstanceState(savedInstanceState);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class BarFragment extends Fragment {
public BarFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.i(LOG_TAG, "Fragment.onCreateView");
View rootView = inflater.inflate(R.layout.fragment_bar, container, false);
return rootView;
}
@Override
public void onCreate(Bundle savedInstanceState) {
Log.i(LOG_TAG, "Fragment.onCreate");
super.onCreate(savedInstanceState);
}
@Override
public void onAttach(Activity activity) {
Log.i(LOG_TAG, "Fragment.onAttach");
super.onAttach(activity);
}
@Override
public void onViewStateRestored(Bundle savedInstanceState) {
Log.i(LOG_TAG, "Fragment.onViewStateRestored");
super.onViewStateRestored(savedInstanceState);
}
@Override
public void onSaveInstanceState(Bundle outState) {
Log.i(LOG_TAG, "Fragment.onSaveInstanceState");
super.onSaveInstanceState(outState);
}
@Override
public void onResume() {
Log.i(LOG_TAG, "Fragment.onResume");
super.onResume();
}
@Override
public void onStart() {
Log.i(LOG_TAG, "Fragment.onStart");
super.onStart();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
Log.i(LOG_TAG, "Fragment.onConfigurationChanged");
super.onConfigurationChanged(newConfig);
}
@Override
public void onPause() {
Log.i(LOG_TAG, "Fragment.onPause");
super.onPause();
}
@Override
public void onStop() {
Log.i(LOG_TAG, "Fragment.onStop");
super.onStop();
}
@Override
public void onDetach() {
Log.i(LOG_TAG, "Fragment.onDetach");
super.onDetach();
}
@Override
public void onDestroy() {
Log.i(LOG_TAG, "Fragment.onDestroy");
super.onDestroy();
}
}
}
答案 1 :(得分:1)
onConfigurationChanged()方法
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
} }
在AndroidManifest.xml中添加此代码
<activity android:name=".MyActivity"
android:configChanges="orientation|keyboardHidden"
android:label="@string/app_name">