gpt4 book ai didi

android - Android 方向改变时执行哪些Activity 的生命周期回调?

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:25:38 30 4
gpt4 key购买 nike

我在调试涉及方向更改和一些空返回的情况时卡住了。

我的问题分为:

1) 当方向改变时,Activity 生命周期的回调会被执行。

2) 当方向改变时,Fragment 的生命周期回调会被执行。

3) 如果我们合并第 2 点和第 3 点(这通常是 fragment 覆盖 Activity 的情况),根据具有 fragment 的 Activity(执行流程?),回调执行流程将是什么。

编辑

如果在 android list 文件中没有定义 configChanges 即:

If any configuration change occurs that is not selected to be reported by that attribute, then instead of reporting it the system will stop and restart the activity (to have it launched with the new configuration).

Activity 和 Fragment 将执行哪些生命周期回调

最佳答案

首先,在 AndroidManifest 中将其添加到您的 Activity 中:android:configChanges="orientation"

1). onConfigurationChanged
2).没什么,但是您可以实现您的方法并从 Activity 的 onConfigurationChanged 中调用它
3).流程将是 Activity.onConfigurationChanged -> Fragment.yourMethod

希望这对您有所帮助。

已更新

这是您第一次开始 Activity 时的流程:

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();
}
}
}

关于android - Android 方向改变时执行哪些Activity 的生命周期回调?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31338683/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com