如何区分活动娱乐是由屏幕旋转,或内存不足的情况是由、屏幕、情况、内存不足

2023-09-07 15:04:25 作者:光脚丫奔跑

据的http://developer.android.com/training/basics/activity-lifecycle/recreating.html

有不同的方式来触发活动的娱乐。

在屏幕旋转 在内存不足的情况​​

我认识的屏幕旋转和内存不足收益率完全不同的行为。

一个明显的意见是,对于恢复从pssed长$ P $ HOME活动,它会破坏并重新创建应用程序为好。

有关屏幕旋转,也不会产生这种行为。

我想知道,怎么能活动片段区分这两种情况?

  @覆盖
保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);

    //检查我们是否再造一个previously破坏实例
    如果(savedInstanceState!= NULL){
        //这是造成屏幕旋转?或者从低内存情况恢复?
        //我们如何区分之间的屏幕旋转,或恢复从内存不足?

    } 其他 {
    }
    ...
}
 
怎么设置surface关闭屏幕旋转屏幕

P / S要生产内存不足的情况​​,在这里要完成的步骤。

preSS家把背部栈的应用程序。 在启动一个内存密集型应用程序。 preSS回家。 重复用于其他应用2-3步5次。 再次启动第一个应用程序。 您会发现 savedInstanceState 不为空。然而,在同一时间,你会发现,目前运行应用程序实例比第一次推出的不同。

除了静态成员将从内存不足的情况​​恢复的时候成为未初始化的,我也遇到一些奇怪的东西

通过 startActivityForResult 启动从父活动的子活动 执行上述6个步骤。 关闭子活动。 我们可以观察到父活动的片段具有下述生命周期。 的onCreate - > onActivityResult - > onResume

我们期待的onCreate - > onResume - > onActivityResult

解决方案   

一个明显的意见是,从pssed住所长期$ P $恢复活动,它会破坏并重新创建应用程序也是如此。

没有,也不会。

如果你的进程被终止,那么当你创建一个新的进程(不管你是如何启动的应用程序),一个新的应用程序被作为其中的一部分创建新进​​程。这并不直接有什么用,从长期$ P $还原活动pssed回家。

  

我可以知道,怎么能活动或片段区分这两种情况?

在理想情况下,他们并不在意。他们会关心的唯一原因是,如果它们都依赖于静态数据成员,其可以或可以不被初始化,在此情况下所使用的那些静态数据成员的方式是有问题的。

我的previous的断言,即 savedInstanceState 将非 - ,是不正确的,该对不起。主场景 savedInstanceState 是一个配置更改。但是,我忘了,从最近的任务列表中展开(恢复从长期pressed家活动)也将通过在上次保存的实例状态。通过其他方式,如从主屏幕发射器发射,将通过在 savedInstanceState

如果你绝对必须区分你的进程,被终止的情况和其他情况,检查一些静态数据成员,看它是否被初始化。

According to http://developer.android.com/training/basics/activity-lifecycle/recreating.html

There are various ways to trigger activity recreation.

Screen rotation Low memory condition

I realize screen rotation and low memory condition yield quite different behavior.

One of the obvious observations is that, for restore activity from long pressed home, it will destroy and re-create Application as well.

For screen rotation, it will not yield such behavior.

May I know, how can Activity or Fragment differentiate both cases?

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Check whether we're recreating a previously destroyed instance
    if (savedInstanceState != null) {
        // Is this caused by screen rotation? Or restoration from low memory condition?
        // How can we differentiate among "screen rotation", or "restoration from low memory condition"?

    } else {
    }
    ...
}

p/s To produce low memory condition, here are the steps to be done.

Press home to put the app in back stack. Launch a memory intensive app. Press home. Repeat steps 2-3 for 5 times for other apps. Launch the 1st app again. You will realize savedInstanceState is not null. However, at the same time, you will realize that current running Application instance is different than the first time launched's.

Besides static members will become uninitialized when restoring from low memory condition, I also encounter some weird stuffs

Launch an child activity from parent activity via startActivityForResult Perform the above 6 steps. Close the child activity. We can observe parent activity's fragment is having the following life cycle. onCreate -> onActivityResult -> onResume

We are expecting onCreate -> onResume -> onActivityResult

解决方案

One of the obvious observations is that, for restore activity from long pressed home, it will destroy and re-create Application as well.

No, it will not.

If your process was terminated, then when a new process is created for you (not matter how you launch the app), a new Application is created as part of that new process. This does not directly have anything to do with "restore activity from long pressed home".

May I know, how can Activity or Fragment differentiate both cases?

Ideally, they don't care. The only reason they would care is if they are dependent upon static data members, which may or may not be initialized, in which case the way that you are using those static data members is problematic.

My previous assertion, that savedInstanceState would be non-null, is incorrect, and for that I apologize. The primary scenario for savedInstanceState is a configuration change. However, I was forgetting that launching from the recent-tasks list ("restore activity from long pressed home") will also pass in the last saved instance state. Launching by other means, such as from the home screen launcher, would pass in null for savedInstanceState.

If you absolutely must distinguish between your-process-was-terminated and other scenarios, check some static data member to see if it is initialized or not.

 
精彩推荐
图片推荐