Nav Component immediately invoked navigate to destination causes a bug
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?

add a comment |
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?

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
– Tomasz Krzyżowski
Nov 19 '18 at 17:23
can you post code relating to yourfindNavController(int id)
and whatnavigateToMyTests()
do?
– davy307
Nov 19 '18 at 17:24
All informations are included in the question
– Tomasz Krzyżowski
Nov 19 '18 at 17:25
add a comment |
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?

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?


edited Nov 19 '18 at 18:27
asked Nov 19 '18 at 16:48


Tomasz Krzyżowski
83
83
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
– Tomasz Krzyżowski
Nov 19 '18 at 17:23
can you post code relating to yourfindNavController(int id)
and whatnavigateToMyTests()
do?
– davy307
Nov 19 '18 at 17:24
All informations are included in the question
– Tomasz Krzyżowski
Nov 19 '18 at 17:25
add a comment |
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
– Tomasz Krzyżowski
Nov 19 '18 at 17:23
can you post code relating to yourfindNavController(int id)
and whatnavigateToMyTests()
do?
– davy307
Nov 19 '18 at 17:24
All informations are included in the question
– Tomasz Krzyżowski
Nov 19 '18 at 17:25
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
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
– Tomasz Krzyżowski
Nov 19 '18 at 17:23
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
– Tomasz Krzyżowski
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
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
– Tomasz Krzyżowski
Nov 19 '18 at 17:25
All informations are included in the question
– Tomasz Krzyżowski
Nov 19 '18 at 17:25
add a comment |
1 Answer
1
active
oldest
votes
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.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53379215%2fnav-component-immediately-invoked-navigate-to-destination-causes-a-bug%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
add a comment |
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.
add a comment |
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.
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.
answered Dec 10 '18 at 9:53


Mohammed
1067
1067
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53379215%2fnav-component-immediately-invoked-navigate-to-destination-causes-a-bug%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
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
– Tomasz Krzyżowski
Nov 19 '18 at 17:23
can you post code relating to your
findNavController(int id)
and whatnavigateToMyTests()
do?– davy307
Nov 19 '18 at 17:24
All informations are included in the question
– Tomasz Krzyżowski
Nov 19 '18 at 17:25