2016-02-18 28 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();方法鎖定這樣的畫面用戶鎖定屏幕,應用程序將完成並需要再次打開。