当方向发生变化时会调用哪种活动方法?

发布于 2024-11-15 11:41:26 字数 173 浏览 3 评论 0原文

当方向发生变化时,会调用生命周期的哪个方法? 我的应用程序执行 onResume() 方法,或者可能重新加载整个活动,因为我设置了一个布尔值来检查它是否是第一次运行。我读过 onConfigurationChanged() 在方向发生变化时启动,这是真的吗? 这要怎么处理呢?

Which method of the lifecycle is called when orientation changes occur?
My application executes the onResume() method or maybe reloads the whole activity because I've set one boolean to check whether it is first run or not. I've read onConfigurationChanged() starts when orientation change occur, is it true?
How to handle this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

葵雨 2024-11-22 11:41:26

有趣的是...

Activity is start onResume() 是您默认在 XML 中声明的。

正如我从堆栈溢出的问题答案中发现的那样,

方向更改

  • onSaveInstanceState
  • onPause
  • onStop
  • onCreate
  • onStart
  • onRestoreInstanceState
  • onResume

Switch TO Activity 2

  • onSaveInstanceState
  • onPause

Orientation Change WHILE IN Activity 2

  • onStop
  • onCreate
  • onStart

Switchback FROM Activity2

  • onResume

我猜测是因为 Activity 1 是在旋转时隐藏,onRestoreInstanceState 不会被调用,因为有没有“视图”(即无法看到/查看)。此外,完全有可能有 2 个完全不同的纵向/横向布局文件,它们可能具有具有不同 ID 的不同 UI 元素。

因此,我想说,如果您想在 onSaveInstanceState 中使用 Bundle 来保存自己的数据,那么您需要在 onCreate 中添加额外的逻辑(在 Activity 中) 1) 在那里处理您自己的数据(以及在 onRestoreInstanceState 中有条件地执行)。

特别是,您可以维护一个“最后已知”方向字段,以便 onCreate 知道它需要处理您自己的数据,因为方向已更改,而不是依赖于 onRestoreInstanceState叫。

Interesting one...

Activity is start onResume() is which you declare in your XML by default.

And as I found from question answer on stack overflow is:

Orientation Change

  • onSaveInstanceState
  • onPause
  • onStop
  • onCreate
  • onStart
  • onRestoreInstanceState
  • onResume

Switch TO Activity 2

  • onSaveInstanceState
  • onPause

Orientation Change WHILE IN Activity 2

  • onStop
  • onCreate
  • onStart

Switchback BACK FROM Activity2

  • onResume

I'm guessing that because Activity 1 is hidden at the time of rotation, onRestoreInstanceState isn't called because there is no 'view' (i.e., it can't be seen/viewed). Also, it is entirely possible to have 2 completely different layout files for portrait/landscape which potentially have different UI elements with different IDs.

As a result, I'd say if you want to use the Bundle in onSaveInstanceState to save your own data, then you need to add extra logic in your onCreate (in Activity 1) to process your own data there (as well as doing it conditionally in onRestoreInstanceState).

In particular, you could maintain a 'last known' orientation field so that onCreate knows that it needs to process your own data because orientation has changed, rather than relying on onRestoreInstanceState being called.

遥远的绿洲 2024-11-22 11:41:26
public class MainActivity extends AppCompatActivity {
private final static String TAG = "AppActivity";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.d(TAG, "onCreate(Bundle) called");
    setContentView(R.layout.activity_main);
}

@Override
public void onStart() {
    super.onStart();
    Log.d(TAG, "onStart() called");
}

@Override
public void onPause() {
    super.onPause();
    Log.d(TAG, "onPause() called");
}

@Override
public void onResume() {
    super.onResume();
    Log.d(TAG, "onResume() called");
}

@Override
public void onStop() {
    super.onStop();
    Log.d(TAG, "onStop() called");
}

@Override
public void onDestroy() {
    super.onDestroy();
    Log.d(TAG, "onDestroy() called");
}

1

) 尝试在手机和/或模拟器上运行您的应用程序并打开 Logcat =>在窗口顶部选择详细。

2) 现在尝试更改屏幕方向(例如从纵向 => 横向模式)。

我希望这个替代方案能让您更深入地了解活动生命周期。

public class MainActivity extends AppCompatActivity {
private final static String TAG = "AppActivity";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.d(TAG, "onCreate(Bundle) called");
    setContentView(R.layout.activity_main);
}

@Override
public void onStart() {
    super.onStart();
    Log.d(TAG, "onStart() called");
}

@Override
public void onPause() {
    super.onPause();
    Log.d(TAG, "onPause() called");
}

@Override
public void onResume() {
    super.onResume();
    Log.d(TAG, "onResume() called");
}

@Override
public void onStop() {
    super.onStop();
    Log.d(TAG, "onStop() called");
}

@Override
public void onDestroy() {
    super.onDestroy();
    Log.d(TAG, "onDestroy() called");
}

}

1) Try to run your app on your phone and/or emulator and open the Logcat => on top of the window select Verbose.

2) Now try to change the screen orientation (ex. from portrait => landscape mode).

I hope this alternative will give you more insight into the activity lifecycle.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文