0

Navigating in onCreate method like so:

override fun onCreate(savedInstanceState: Bundle?) {
 super.onCreate(savedInstanceState)
 setContentView(R.layout.magic_mile_host)
 setSupportActionBar(toolbar_start_test)
 supportActionBar?.setDisplayHomeAsUpEnabled(true)
 supportActionBar?.setHomeAsUpIndicator(R.drawable.ic_arrow_back_black_24dp)
 navController = findNavController(R.id.nav_host_magic_mile)
 navigateToMyTests()
}

Here is my navigateToMyTests() implementation

navController.navigate(R.id.myTestsFragment)

The problem only appears when i invoke this function immediately. The problem is when I'm on fragment which i came from myTestsFragment. After rotating screen the current fragment is not restored but myTestsFragment is restored always.

The reason why I did this way is because i want to ommit my startDestination in nav graph in certain situation. Could you explain me why it's happening and maybe help me to come up with other solution to this problem?

TMaszko
  • 13
  • 3
  • not sure i understand your problem, are you calling test code from within your UI? That's a big code smell and should be avoided at all costs... That aside, is the issue that on rotation of your device you're loosing the fragment? – davy307 Nov 19 '18 at 17:10
  • this is not testing code. I'm beginner with android ;). Yes it's returning always to the myTestsFragment even if I 'm currently on the other location – TMaszko Nov 19 '18 at 17:23
  • can you post code relating to your `findNavController(int id)` and what `navigateToMyTests()` do? – davy307 Nov 19 '18 at 17:24
  • All informations are included in the question – TMaszko Nov 19 '18 at 17:25

1 Answers1

0

In your case, which is about setting your start destination it's better to change it when it's needed using this line of code: navController.getGraph().setStartDestination(int id); Another point you should pay attention is that calling your navigation methods inside the onCreate() in your Activity is risky, as the navHost so the FragmentManager might not be ready yet. Make sure your start destination is attached, then start your navigation process.

Mohammed
  • 126
  • 7