Omkar Amberkar
10/29/2023, 3:51 AMclass MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
WindowCompat.setDecorFitsSystemWindows(window, false)
setContent {
PictureInPicturePOCTheme {
val navController = rememberNavController()
val navigationActions = remember(navController) {
PictureInPicturePOCNavigationActions(navController)
}
// A surface container using the 'background' color from the theme
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
PictureInPicturePOCNavGraph(
modifier = Modifier.fillMaxSize(),
navController = navController,
navigateToDetails = navigationActions.navigateToDetails,
navigateToPlayer = navigationActions.navigateToPlayer
)
}
}
}
}
}
object PictureInPicturePOCDestinations {
const val HOME_ROUTE = "home"
const val DETAILS_ROUTE = "details"
const val PLAYER_ROUTE = "player"
}
class PictureInPicturePOCNavigationActions(navController: NavHostController) {
val navigateToHome: () -> Unit = {
navController.navigate(PictureInPicturePOCDestinations.HOME_ROUTE) {
// Pop up to the start destination of the graph to
// avoid building up a large stack of destinations
// on the back stack as users select items
popUpTo(navController.graph.findStartDestination().id) {
saveState = true
}
// Avoid multiple copies of the same destination when
// reselecting the same item
launchSingleTop = true
// Restore state when reselecting a previously selected item
restoreState = true
}
}
val navigateToDetails: () -> Unit = {
navController.navigate(PictureInPicturePOCDestinations.DETAILS_ROUTE) {
launchSingleTop = true
restoreState = true
}
}
val navigateToPlayer: () -> Unit = {
navController.navigate(PictureInPicturePOCDestinations.PLAYER_ROUTE) {
launchSingleTop = true
restoreState = true
}
}
}
@Composable
fun PictureInPicturePOCNavGraph(
modifier: Modifier = Modifier,
navController: NavHostController = rememberNavController(),
startDestination: String = PictureInPicturePOCDestinations.HOME_ROUTE,
navigateToDetails: () -> Unit,
navigateToPlayer: () -> Unit
) {
NavHost(
navController = navController,
startDestination = startDestination,
modifier = modifier
) {
composable(PictureInPicturePOCDestinations.HOME_ROUTE) {
HomeRoute(
navigateToDetails = navigateToDetails,
navigateToPlayer = navigateToPlayer
)
}
composable(PictureInPicturePOCDestinations.DETAILS_ROUTE) {
DetailsRoute(
navigateToDetails = navigateToDetails,
navigateToPlayer = navigateToPlayer
)
}
activity(PictureInPicturePOCDestinations.PLAYER_ROUTE) {
activityClass = PlayerActivity::class
}
}
}
@Composable
fun HomeRoute(
navigateToDetails: () -> Unit,
navigateToPlayer: () -> Unit
) {
Column(
Modifier
.fillMaxSize()
.background(Color.White)
.padding(100.dp)
) {
Button(modifier = Modifier.wrapContentSize(), onClick = { navigateToDetails.invoke() }) {
Text(text = "navigate to details")
}
Button(modifier = Modifier.wrapContentSize(), onClick = { navigateToPlayer.invoke() }) {
Text(text = "navigate to player")
}
}
}
@Composable
fun DetailsRoute(
navigateToDetails: () -> Unit,
navigateToPlayer: () -> Unit
) {
Column(
Modifier
.fillMaxSize()
.background(Color.Yellow)
.padding(100.dp)
) {
Button(modifier = Modifier.wrapContentSize(), onClick = { navigateToDetails.invoke() }) {
Text(text = "navigate to details")
}
Button(modifier = Modifier.wrapContentSize(), onClick = { navigateToPlayer.invoke() }) {
Text(text = "navigate to player")
}
}
}
class PlayerActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
WindowCompat.setDecorFitsSystemWindows(window, false)
setContent {
PictureInPicturePOCTheme {
val navController = rememberNavController()
val navigationActions = remember(navController) {
PictureInPicturePOCNavigationActions(navController)
}
// A surface container using the 'background' color from the theme
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
PlayerRoute(
navigateToDetails = navigationActions.navigateToDetails,
enterPip = { enterPictureInPictureMode() }
)
}
}
}
}
}
@Composable
fun PlayerRoute(
navigateToDetails: () -> Unit,
enterPip: () -> Unit
) {
Column {
Box(
Modifier
.aspectRatio(1.7777778F)
.background(Color.Black)
) {
Button(modifier = Modifier
.wrapContentSize()
.align(Alignment.Center),
onClick = { enterPip.invoke() }
) {
Text(text = "enter pip")
}
}
Box(
Modifier
.fillMaxSize()
.background(Color.White)
) {
Button(
modifier = Modifier
.wrapContentSize()
.align(Alignment.Center),
onClick = {
enterPip.invoke()
navigateToDetails.invoke()
}
) {
Text(text = "navigate to details")
}
}
}
}
Ian Lake
10/29/2023, 5:59 AMnavigate
that is going to go back to the previous activity - consider every activity as completely separate from one another. Your other activity should already be below your PIP activity anyways, so entering PIP should automatically make your other activity visible right?Omkar Amberkar
10/29/2023, 1:56 PMIan Lake
10/29/2023, 3:08 PMOmkar Amberkar
10/29/2023, 5:02 PMIan Lake
10/29/2023, 5:06 PMOmkar Amberkar
10/29/2023, 5:08 PMIan Lake
10/29/2023, 5:12 PMOmkar Amberkar
10/29/2023, 5:29 PM