1

I implemented Rewarded Interstitial Ad in my jetpack project. I used the second activity to show the ad and now I want to implement OnUserEarnedRewardListener. My user can see the last screen after the ad finishes. I find it challenging to navigate from compos to activity and conversely with an argument. These are my Screen and NavGraph.

sealed class Screens (val route: String){
    object SMasks: Screens("s_masks")
    object BMasks: Screens("b_masks")
    object FinalShow: Screens("final_show")
    object ProgressBar: Screens("progress_bar")
    object RewardedShow: Screens("rewarded_show")

}

and this is my NavGraph

@SuppressLint("UnrememberedMutableState")
@Composable
fun MyNavGraph(

navController: NavHostController

) {
    val actions = remember(navController) { MainActions(navController) }

NavHost(
    navController = navController,
    startDestination = BottomNavItems.SMasks.route
) {
    composable(BottomNavItems.SMasks.route) {
        SMaskScreen(actions
        )
    }

    composable(
        Screens.BMasks.route
    ) {

        BMasksScreen(navController, actions)
    }
   
    composable("${Screens.FinalShow.route}/{maskArg}") {
        val maskArg = it.arguments?.getString("maskArg")
        if (maskArg != null) {
            FinalShowScreen(
                maskArg = maskArg, navController
            )
        }
     }

   }

 }

} 

class MainActions(navController: NavController) {

val gotoFinalShow: (String) -> Unit = { maskArg ->
    navController.navigate("${Screens.FinalShow.route}/$maskArg") {
        launchSingleTop = true
        restoreState = true
    }
}

this is my code that when the user clicks on it navigate to RewardShowActivity.

 onItemClick =  {
                    maskArg = "mask1"
                    goToActivity(maskArg = maskArg, context = context)
                }

fun goToActivity(context: Context, maskArg: String) {
val intent = Intent(context, RewardedShowActivity::class.java)
intent.putExtra("maskArg", maskArg)
context.startActivity(intent)
}

and this is my RewardedShowActivity

class RewardedShowActivity : ComponentActivity(), OnUserEarnedRewardListener {

val loading = mutableStateOf(true)

private companion object{
    const val TAG = "TAG"
}
private var mRewardedInterstitialAd: RewardedInterstitialAd? = null


@SuppressLint("UnrememberedMutableState")
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    val maskArg =  intent.extras!!.getString("maskArg")

    setContent {

        MyAppTheme {

            Surface(
                modifier = Modifier.fillMaxSize(),
                color = MaterialTheme.colorScheme.background
            ) {
                CircleIndeterminateProgressBar(isDisplayed = 
loading )

            }
        }
    }

override fun onUserEarnedReward(p0: RewardItem) {
  ??????????
    }
}
razi
  • 77
  • 8

1 Answers1

3

There are ways to navigate from composable to another activity e.g. here But the beauty of Compose is in having all your code within Compose, so if you show your ad as a composable and not a separate activity then you won't have any issues. Can you rethink your code design so there is only a single Activity? (which is by the way the current google-recommended way)

David Aleksanyan
  • 2,953
  • 4
  • 29
  • 39
  • Thanks, @David A. Actually, I am trying to implement it in compoe but have faced some errors. For example – razi Sep 01 '22 at 10:48
  • That's fine, we're all learning Compose, it's somewhat new. Did you create a question with the errors you faced, I can have look. – David Aleksanyan Sep 01 '22 at 11:16
  • 1
    thanks, @David d. just implemented it successfully in compose. thanks – razi Sep 01 '22 at 14:11