CopyPastor

Detecting plagiarism made easy.

Score: 1; Reported for: Exact paragraph match Open both answers

Possible Plagiarism

Plagiarized on 2023-09-05
by molokoka

Original Post

Original - Posted on 2021-09-07
by Phil Dukhov



            
Present in both answers; Present only in the new answer; Present only in the old answer;

answered here by Phil Dukhov:
https://stackoverflow.com/a/69083009/1282732

``` @Composable fun LockScreenOrientation(orientation: Int) { val context = LocalContext.current DisposableEffect(Unit) { val activity = context.findActivity() ?: return@DisposableEffect onDispose {} val originalOrientation = activity.requestedOrientation activity.requestedOrientation = orientation onDispose { // restore original orientation when view disappears activity.requestedOrientation = originalOrientation } } }
fun Context.findActivity(): Activity? = when (this) { is Activity -> this is ContextWrapper -> baseContext.findActivity() else -> null } ```
You can do this with `DisposableEffect` + activity `requestedOrientation`. ``` @Composable fun LockScreenOrientation(orientation: Int) { val context = LocalContext.current DisposableEffect(Unit) { val activity = context.findActivity() ?: return@DisposableEffect onDispose {} val originalOrientation = activity.requestedOrientation activity.requestedOrientation = orientation onDispose { // restore original orientation when view disappears activity.requestedOrientation = originalOrientation } } }
fun Context.findActivity(): Activity? = when (this) { is Activity -> this is ContextWrapper -> baseContext.findActivity() else -> null } ``` Usage: ``` @Composable fun Screen() { LockScreenOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) } ```

        
Present in both answers; Present only in the new answer; Present only in the old answer;