Home > Blockchain >  Can't add action to Mainactivity since it does not support actions
Can't add action to Mainactivity since it does not support actions

Time:11-16

I'm working on this app and I've created the login and registration fragments and a main activity. I want to show the login fragment first thing after the application launches but I keep having this error and the app crashes:

UnsupportedOperationException: Cannot add action 2131362356 to Destination(/mainActivity) label=MainActivity class=MainActivity as it does not support actions, indicating that it is a terminal destination in your navigation graph and will never trigger actions.

here is my main.kt

class MainActivity :AppCompatActivity (){

    override fun onCreate(savedInstanceState: Bundle? ) {
  super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main);
supportActionBar?.hide()
val navController = findNavController(this,R.id.nav_host_fragment)
navController.navigate(R.id.loginFragment)

}

and my navigation file

<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@ id/navigation"
app:startDestination="@id/mainActivity">

<fragment
    android:id="@ id/loginFragment"
    android:name="com.exa.activities.login.LoginFragment"
    android:label="login_fragment"
    tools:layout="@layout/login_fragment" >
    <action
        android:id="@ id/action_loginFragment_to_registrationFragment"
        app:destination="@id/registrationFragment" />
</fragment>

<activity
    android:id="@ id/mainActivity"
    android:name="com.exa.MainActivity"
    android:label="MainActivity" >
<action
    android:id="@ id/action_main_to_loginFragment"
    app:destination="@id/loginFragment"
    />
</activity>

I've tried the basics such as invalidation cache/restarting and checking the names and the launching activity for the app but nothing changed. I want to show the LoginFragment first thing and then other screens as the user logs in.

CodePudding user response:

From the docs:

Note: The Navigation component is designed for apps that have one main activity with multiple fragment destinations. The main activity is associated with a navigation graph and contains a NavHostFragment that is responsible for swapping destinations as needed. In an app with multiple activity destinations, each activity has its own navigation graph.

The idea is that you have an activity which is a NavHost, which acts as a container. The destinations are all Fragments, and get swapped in and out of the container to "navigate" through the app. Since you're trying to use an Activity as a destination in the nav graph, I'm assuming that's your problem - your destinations need to be fragments (including your startDestination).

Also since you're doing the login thing, there's a tutorial on handing that exact use case: https://developer.android.com/guide/navigation/navigation-conditional

Bear in mind that your startDestination is basically at the bottom of the backstack, and can't be popped off - it's the last thing the user sees if they back out of the app (and if it automatically forwards them to another fragment, they won't be able to back out). Just something to keep in mind if you want the login screen to be the first thing they see (if you're going to handle the case where they're already logged in by automatically navigating somewhere else)

CodePudding user response:

Remove all activity tags from your navigation graph. Change app:startDestination="@id/mainActivity" to app:startDestination="@id/loginFragment"

  • Related