Search code examples
androidandroid-activityback-stack

How to provide back navigation in a sub-flow with activities


I have Activities A, B, C which between the user can navigate.

Activity B also provides a sub-flow like (B1, B2, B3). When the user completes the subflow, he is sent back to B with some data.

What intent flags or launch modes should I use to provide back navigation between A - B - C, and between B1 - B2 - B3. But also clear/finish this sub flow when returning to B with some data passed in the intent?

I have tried adding FLAG_ACTIVITY_CLEAR_TOP & FLAG_ACTIVITY_SINGLE_TOP to the sub activities (B1, B2, B3) but that didn't work.

visual explanation


Solution

  • In the end I managed to get the desired result by doing the following. In Activity B, I created 2 methods to start the activity

    class ActivityB : AppCompatActivity() {
    
        fun startFromActivityA(context: Context) {
            val intent = Intent(context, ActivityB::class.java)
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            context.startActivity(intent)
        }
    
        fun startFromActivityB2(context: Context, intentData: IntentData) {
            val intent = Intent(context, ActivityB::class.java)
            intent.putExtra(INTENT_DATA, intentData)
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK)
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
            context.startActivity(intent)
        }
    }
    

    So when I enter the subflow and i want to finish the subflow, I would call startFromActivityB2. It would clear the whole subflow and still provide back navigation to Activity A