I want to keep the screen on for the duration of my activity's lifecycle. We'll call this Activity A. I do so by setting
android:keepScreenOn="true"
in the xml. This works perfectly on most screens. However, Activity A launches Activity B, which is used in other parts of the app. Is there a way to force Activity B to keep the screen on when launched from Activity A? I've read that I should avoid using wake lock; I'm curious what others' opinions are on this.
You can set it programmatically with:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
If you only want Activity B to keep the screen on when it's launched from Activity A you can achieve it like this:
When A launches B, send an extra value on the intent, On B, onCreate() check if there is a value sent and only if it founds something it's because came from A.
public class ActivityB extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_b);
Bundle extras = getIntent().getExtras();
if(extras != null)
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
}