简体   繁体   English

屏幕锁定时活动返回到纵向

[英]Activity return to Portrait when Screen is Locked

Related questions: 相关问题:


This is a strange behaviour i got in my Activities. 这是我在我的活动中遇到的奇怪行为。

Portrait mode (It's normal) 肖像模式(这是正常的)

  1. Press screen lock, Activity: onPause() ; 按屏幕锁定, Activity: onPause() ;
  2. Unlock the screen, Activity: onResume() . 解锁屏幕, Activity: onResume()

Landscape mode (It's strange) 风景模式(很奇怪)

  1. Press screen lock, Activity: onPause() -> onStop() -> onDestroy() -> onCreate() -> onStart() -> onResume() which loads the Portrait layout; 按屏幕锁定, Activity: onPause() -> onStop() -> onDestroy() -> onCreate() -> onStart() -> onResume()加载Portrait布局;
  2. Unlock the screen, Activity: onPause() -> onStop() -> onDestroy() -> onCreate() -> onStart() -> onResume() and loads the Landscape layout. 解锁屏幕, Activity: onPause() -> onStop() -> onDestroy() -> onCreate() -> onStart() -> onResume()并加载Landscape布局。

What I expect is: 我的期望是:

Portrait mode: (same) 人像模式:(相同)

Landscape mode: (should act like Portrait mode) 风景模式:(应该像肖像模式一样)

  1. Press screen lock, Activity: onPause() ; 按屏幕锁定, Activity: onPause() ;
  2. Unlock the screen, Activity: onResume() . 解锁屏幕, Activity: onResume()

So my questions: 所以我的问题:

  • Why does my Activities behave like this? 为什么我的活动表现得像这样?
  • How does your Activities behave? 您的活动如何表现?

Original text description of my question: 我的问题的原始文字说明:

While i press the Lock Screen button of my phone, when my Activity is at its Landscape mode, i noticed (in the debug messages i output to Eclipse) the Activity is re-created to its Portrait mode (while the screen is all black, of course). 当我按下手机的锁屏按钮时,当我的Activity处于其横向模式时,我注意到(在我输出到Eclipse的调试消息中),Activity被重新创建为其Portrait模式(而屏幕全是黑色,当然)。 Then when i press the Lock Screen button again to unlock the screen, the Activity was destroyed and re-created to its Portrait again. 然后当我再次按下锁定屏幕按钮解锁屏幕时,活动被销毁并再次重新创建其肖像。

As i remember (not 100% sure though), and what i expect is, my Activity should only undergo onSaveInstanceState() and onPause() , while Lock Screen in Landscape mode, like what it does in Portrait mode. 正如我记得的那样(虽然不是100%肯定),而且我期望的是,我的Activity应该只进行onSaveInstanceState()onPause() ,而在横向模式下使用Lock Screen,就像在Portrait模式下一样。 Rather than re-creating the Activity to Portrait and going back to Landscape again. 而不是重新创建活动到肖像并再次回到景观。

Is it that i have messed up something with my phone? 是不是我用手机搞砸了什么? How can i fix it back to normal? 我怎样才能恢复正常?

Thanks! 谢谢!


Thanks everyone for contributing into this issue. 感谢大家为这个问题做出贡献。 Especially thanks @HoanNguyen for his effort of testing for me in his devices. 特别感谢@HoanNguyen在他的设备中为我测试的努力。 And Especially thanks @Raghunandan for having an in-depth discussion with me concerning this issue. 特别感谢@Raghunandan就这个问题与我进行了深入的讨论。

Summarising everyone's contributions so far, i have the following conclusions: 总结到目前为止每个人的贡献,我得出以下结论:

1. This is a normal phenomenon. 这是正常现象。

It seems that, on mobile phones, the running Activities, that are in Landscape mode, are switched into Portrait mode upon screen lock is a normal behaviour. 似乎在手机上,在横向模式下运行的活动在屏幕锁定是正常行为时切换到纵向模式。 At least it is true on the tested phones so far. 至少到目前为止在测试手机上都是如此。 So we have to make sure our lifecycle functions can take care this change elegantly always. 因此,我们必须确保我们的生命周期功能始终能够优雅地关注这一变化。

2. Guess this is because of the "default orientation" in the locked screen. 2.猜测这是因为锁定屏幕中的“默认方向”。

We do not have documentation or many resource talking about this issue. 我们没有关于此问题的文档或许多资源。 But the assumption that the running Activities switching back to the device's "default orientation" upon screen lock, as in most devices the locked screen is in Portrait, is quite logical. 但是假设在屏幕锁定时运行的活动切换回设备的“默认方向”,就像在大多数设备中锁定的屏幕是纵向一样,这是非常合乎逻辑的。

Further study: 进一步研究:

I just wonder how the Activities behave if we are having a landscape locked screen? 我只是想知道如果我们有一个横向锁定屏幕,活动会如何表现?

You can stop the activity from restarting on orientation change but this is generally a pretty bad idea. 您可以停止活动重定向更改,但这通常是一个非常糟糕的主意。

The Android documentation has a section on handling runtime changes with this note: Android文档有一个关于使用此注释处理运行时更改的部分:

Note: Handling the configuration change yourself can make it much more difficult to use alternative resources, because the system does not automatically apply them for you. 注意:自行处理配置更改会使使用备用资源变得更加困难,因为系统不会自动为您应用它们。 This technique should be considered a last resort when you must avoid restarts due to a configuration change and is not recommended for most applications. 当您必须避免因配置更改而重新启动时,此技术应被视为最后的手段,并且不建议用于大多数应用程序。

Android generally only recommends you supress recreating on rotation if you don't need alternate resources and , more importantly, have a performance requirement. 如果您不需要备用资源, 并且更重要的是,具有性能要求,Android通常仅建议您在轮换时进行重新创建。 A well-designed app shouldn't need to do this in most cases. 在大多数情况下,精心设计的应用程序不需要这样做。

If you insist on going down the path of supressing default Android behavior, I'd modify Raghunandan's code and include a screen size attribute as well. 如果你坚持沿着压制默认Android行为的道路,我会修改Raghunandan的代码并包含一个屏幕尺寸属性。 As of API level 13, the screen size changes upon orientation change. 从API级别13开始,屏幕尺寸在方向改变时改变。 So you must include screenSize unless you are only targeting API 12 and below. 因此,除非您仅定位API 12及更低版本,否则必须包含screenSize。

<activity android:name=".MyActivity"
      android:configChanges="orientation|screenSize"
      android:label="@string/app_name">

To avoid activity from restarting 避免重新启动活动

<activity android:name=".MyActivity"
      android:configChanges="orientation|keyboardHidden"//add tthis in manifest
      android:label="@string/app_name">

http://developer.android.com/guide/topics/resources/runtime-changes.html . http://developer.android.com/guide/topics/resources/runtime-changes.html

In normal circumstances when your screen is locked your activity is paused and when screen is unlocked activity resumes. 在正常情况下,当您的屏幕被锁定时,您的活动会暂停,当屏幕解锁时,活动会恢复。

An issue when screen locked is: Current Activity may be stopped forcefully by the system if it finds shortage of memory, instead of moving Activity to background . 屏幕锁定时的问题是: 如果发现内存不足,系统可能会强制停止当前活动,而不是将活动移动到后台 In such a case, we should have to save (all the necessary data) the current state of the Activity. 在这种情况下,我们应该保存(所有必要的数据)Activity的当前状态。

Save you data in onSaveInstanceState() and restore data onRestoreInstanceState(). 将数据保存在onSaveInstanceState()中并恢复数据onRestoreInstanceState()。

@Override
 public void  onSaveInstanceState(Bundle outState)
 {
  Log.v("$````$", "In Method: onSaveInstanceState()");
  //if necessary,set a flag to check whether we have to restore or not
  //handle necessary savings…
 }

@Override
public void onRestoreInstanceState(Bundle inState)
{
  Log.v("$````$", "In Method: onRestoreInstanceState()");
  //if any saved state, restore from it…
}

In your onCreate() 在你的onCreate()

IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
filter.addAction(Intent.ACTION_USER_PRESENT);

mReceiver = new ScreenReceiver();
registerReceiver(mReceiver, filter); //register


public class ScreenReceiver extends BroadcastReceiver {

  @Override
  public void onReceive(Context context, Intent intent)
   {
        if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF))
        {    
              Log.v("$$$$$$", "In Method:  ACTION_SCREEN_OFF");
              // onPause() will be called.
        }
        else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON))
        {
              Log.v("$$$$$$", "In Method:  ACTION_SCREEN_ON");
              //onResume() will be called.
              //Better check for whether the screen was already locked
              // if locked, do not take any resuming action in onResume()
              //Suggest you, not to take any resuming action here.       
        }
        else if(intent.getAction().equals(Intent.ACTION_USER_PRESENT))
        {
              Log.v("$$$$$$", "In Method:  ACTION_USER_PRESENT"); 
              //Handle resuming events
        }
  }

In your onDestroy 在你的onDestroy中

  @Override
  public void onDestroy()
  {
        super.onDestroy();
        Log.v("$$$$$$", "In Method: onDestroy()");

        if (mReceiver != null)
        {
              unregisterReceiver(mReceiver); //unregister 
              mReceiver = null;
        }          

  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM