0

How i can move from SettingsListFragment in my MainActivity?

<fragment
        android:id="@+id/settings_list_fragment"
        android:name="com.mandarine.targetList.features.settings.SettingsListFragment"
        android:label="@string/settings"
        tools:layout="@layout/fragment_settings_list">
    </fragment>

Because in my navigation graph i haven't screen from MainActivity. Whole navigation i have in my one activity, u can check code

class MainActivity : AppCompatActivity(), MainActivityViewContract {

    private lateinit var auth: FirebaseAuth
    private lateinit var mAuthStateListener: FirebaseAuth.AuthStateListener
    private val presenter = MainActivityPresenter(contract = this)
    private lateinit var appBarConfiguration: AppBarConfiguration

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        setSupportActionBar(toolbar)
        signIn()
        setupViews()
    }

    override fun onSupportNavigateUp(): Boolean {
        return findNavController(R.id.nav_host_fragment).navigateUp(appBarConfiguration)
    }

    override fun onResume() {
        super.onResume()
        auth.addAuthStateListener(mAuthStateListener)
    }

    override fun onPause() {
        super.onPause()
        auth.removeAuthStateListener(mAuthStateListener)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        presenter.onActivityResult(requestCode, resultCode)
    }

    override fun cancelSignIn() {
        finish()
    }

    private fun setupViews() {
        val host: NavHostFragment = supportFragmentManager
            .findFragmentById(R.id.nav_host_fragment) as NavHostFragment? ?: return

        val navController = host.navController
        appBarConfiguration = AppBarConfiguration(navController.graph)

        val drawerLayout: DrawerLayout? = findViewById(R.id.drawer_layout)
        appBarConfiguration = AppBarConfiguration(
            setOf(R.id.target_list, R.id.settings_list_fragment, R.id.calendar_fragment),
            drawerLayout
        )
        setupActionBar(navController, appBarConfiguration)
        setupNavigationMenu(navController)
        setupBottomNavMenu(navController)
    }

    private fun setupNavigationMenu(navController: NavController) {
        val sideNavView = findViewById<NavigationView>(R.id.nav_view)
        sideNavView?.setupWithNavController(navController)
    }

    private fun setupActionBar(
        navController: NavController,
        appBarConfig: AppBarConfiguration
    ) {
        setupActionBarWithNavController(navController, appBarConfig)
    }

    private fun setupBottomNavMenu(navController: NavController) {
        val bottomNavigationView = findViewById<BottomNavigationView>(R.id.bottom_navigation_view)
        bottomNavigationView?.setupWithNavController(navController)
    }

    private fun signIn() {
        auth = FirebaseAuth.getInstance()
        mAuthStateListener = FirebaseAuth.AuthStateListener { firebaseAuth ->
            presenter.signIn(activity = this, user = firebaseAuth.currentUser)
        }
    }
}
Morozov
  • 4,968
  • 6
  • 39
  • 70

2 Answers2

0

You can add an <activity../> tag in your navigation graph and set it as destination in actions.

<activity
  android:id="@+id/main_activity_destination"
  android:name="com.mandarine.your.package.path.here.MainActivity" />

Then, you can call navController.navigate(R.id.main_activity_destination)

Or, if you want more control over the flow from the nav graph, you can create an action:

<action
  android:id="@+id/go_to_main_activity"
  app:clearTask="true"
  app:destination="@+id/main_activity_destination"
  app:launchSingleTop="true"
  app:popUpTo="@id/main_activity_destination" />

Then, you can call navController.navigate(R.id.go_to_main_activity)

Mircea Nistor
  • 3,145
  • 1
  • 26
  • 32
  • I can't add `` tag in my navigation Because in project i have 1 activity and this whole logic in `MainActivity` Also i added my activity in question, now u can check it. – Morozov Nov 04 '19 at 11:51
  • My initial understanding of your question was that you want to move TO MainActivity from SettingsListFragment. Now, after the edits you made to it, it's unclear where you want to navigate to, since you are already in your MainActivity. Please describe what screen is your destination – Mircea Nistor Nov 04 '19 at 15:57
0

The question is not completely clear, but it seems like what you are trying to achieve is fragment navigation using NavController. Before answering the question, let me give you a brief understanding of how the new jetpack navigation works.

You will be having a single activity in your application(can have multiple activities as well, but for the sake of explaining, I am sticking with one). In this particular activity, you are having a single NavigationHostFragment, which is responsible for all your app navigation. This NavigationHostFragment will be equivalent to the container for adding Fragments in older way of doing it. The NavigationHostFragment will have a navigation controller, which will be linked to the storyboard. The navigation.xml file which you referred in the question is your storyboard.

The storyboard will contain all the fragments which you need in the navigation and the navigation directions as well. The navigation directions is referred to as actions. Each action in a fragment will be a navigation to a separate fragment.

Hence the navigation has nothing to do with Activity, it is all happening in the NavigationHostFragment(which I hope you already have in place in your Activity's xml file).

With these fundamentals, with two fragments, in which FragmentA is navigating to FragmentB, your navigation xml file will look somewhat like this:

<fragment android:id="@+id/fragmentA" android:name="com.xxx.xxx.FragmentA"
          android:label="fragment_a" tools:layout="@layout/fragment_a">
    <action android:id="@+id/action_fragmentA_to_fragmentB"
            app:destination="@id/fragmentB"/>
</fragment>
<fragment android:id="@+id/fragmentB"
          android:name="com.xxx.xxx.FragmentB"
          android:label="fragment_b" tools:layout="@layout/fragment_b">
</fragment>

And then in FragmentA code, where you need to perform the navigation, just call:

findNavController().navigate(R.id.action_fragmentA_to_fragmentB)
Harikrishnan
  • 7,765
  • 13
  • 62
  • 113