hotwired / turbo-android

Android framework for making Turbo native apps
MIT License
423 stars 51 forks source link

No back button when MainActivity is not a TurboActivity #227

Closed annajeanine closed 2 years ago

annajeanine commented 2 years ago

In the current app I'm working on the setup differs from the demo setup. In the demo setup, the MainActivity uses the TurboActivity class. In my current setup, I have native authentication and therefore the MainActivity handles part of the authentication. When the user is authenticated, they are passed to the WebActivity. The WebActivity contains all needed and uses the WebFragment as in the demo.

However, when testing the app right now, the 'back' button has disappeared. I have searched the source code, but no luck. Is the TurboNavGraphBuilder required to use the MainActivity as registered activity? I have tried registering the WebActivity, but no luck.

Could someone help me out with this?

jayohms commented 2 years ago

Hey @annajeanine, it's possible to have multiple TurboActivity instances in your app. The library provides support for all-native TurboFragments in your MainActivity (with its own TurboSessionNavHostFragment). https://github.com/hotwired/turbo-android/blob/main/docs/ADVANCED-OPTIONS.md#using-multiple-activities

However, if you don't want to go down that path and you want MainActivity to be totally separate, you could add the following in your MainActivity:

import androidx.activity.addCallback

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        onBackPressedDispatcher.addCallback(this) {
            // TODO navigate to `WebActivity`
            finish()
        }
    }

    private fun setupToolbar() {
        val toolbar: Toolbar = // TODO get reference to the toolbar in your activity layout

        toolbar.setNavigationIcon(R.drawable.ic_back) // TODO you'll need to provide a back button icon
        toolbar.setNavigationOnClickListener {
            if (onBackPressedDispatcher.hasEnabledCallbacks()) {
                onBackPressedDispatcher.onBackPressed()
            }
        }
    }
}

Hope that helps! Using an Activity that doesn't depend on TurboActivity is out of scope of the library and support here, but you have full control on everything in that Activity (turbo-android won't play a role at all).

annajeanine commented 2 years ago

@jayohms Thank you! Managed to fixed it with the following code in my WebFragment

toolbarForNavigation()?.apply {
    inflateMenu(R.menu.web)
    setNavigationIcon(R.drawable.ic_round_arrow_back_24)
    setNavigationOnClickListener {
        navigateBack()
    }
}

Works like a charm for anyone interested 👍

kyrylo commented 8 months ago

Could you please provide an example of how to use multiple activities?

I followed the multiple activities doc, but after annotating them, I got an error:

Caused by: java.lang.IllegalArgumentException:
A start Fragment destination was not found for uri: turbo://activity/login
@TurboNavGraphDestination(uri = "turbo://activity/main")
class MainActivity : AppCompatActivity(), TurboActivity {
@TurboNavGraphDestination(uri = "turbo://activity/login")
class LoginActivity : AppCompatActivity(), TurboActivity {
{
  "patterns": [
    "^/sign_in$",
    "^/sign_up$"
  ],
  "properties": {
    "uri": "turbo://activity/login",
    "pull_to_refresh_enabled": false
  }
},

From what I understand, I need to create a res/navigation file, but I only want to hide the bottom navigation on my login pages (and display it when you're signed in). Not sure if this is the way to go, any help is greatly appreciated!