2016-02-18 31 views
3

我已经为清单上的Activity设置了noHistory="true"。预期的结果是当用户离开Activity并且它不再在屏幕上可见时,活动将完成。当屏幕锁定时,活动noHistory =“true”不起作用

这个工作正常,当我导航到不同的应用程序,按Home按钮并返回到活动按预期重新创建。但是,当Activity是可见的,如果屏幕锁定,并解锁回resumes the activity。我想Activity重新创建或只是不显示,让用户再次启动应用程序。

<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name=".TestActivity" 
     android:label="@string/app_name" 
     android:screenOrientation="portrait" 
     android:noHistory="true"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
</application> 

回答

1

我想Activity重建或只是没有显示出来,让用户 重新启动应用程序。

我相信你能处理如果屏幕被锁定,然后做你的东西一样杀死app.Seems当用户锁定屏幕,ActivityonPause();法会等。

这里是logcat的:,

public void onPause() { 
     super.onPause(); 

     Log.e(tag, "In the onPause() event"); 
     // If the screen is off then the device has been locked 
     PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE); 
     boolean isScreenOn = powerManager.isScreenOn(); // deprecated, but you can use isInteractive too 

     if (!isScreenOn) { 

      finish(); // or do your stuffs 
     } 

    } 

这时如果:

02-18 20:27:23.621 22250-22250/com.client.stackoveflow E/LifeCycleEvents: In the onCreate() event 
02-18 20:27:23.621 22250-22250/com.client.stackoveflow E/LifeCycleEvents: In the onStart() event 
02-18 20:27:23.626 22250-22250/com.client.stackoveflow E/LifeCycleEvents: In the onResume() event 
02-18 20:27:27.156 22250-22250/com.client.stackoveflow E/LifeCycleEvents: In the onPause() event 
02-18 20:27:27.161 22250-22250/com.client.stackoveflow E/LifeCycleEvents: In the onStop() event 
02-18 20:27:36.866 22250-22250/com.client.stackoveflow E/LifeCycleEvents: In the onRestart() event 
02-18 20:27:36.866 22250-22250/com.client.stackoveflow E/LifeCycleEvents: In the onStart() event 
02-18 20:27:36.881 22250-22250/com.client.stackoveflow E/LifeCycleEvents: In the onResume() event 

因此,终止该应用/或做你的东西,当用户在onPause();方法锁定这样的画面用户锁定屏幕,应用程序将完成并需要再次打开。