Navigating back to previous Activity












2















I have two Activities A and B.



Activity A has a Tablayout with some Tabs. When I navigate from A to B I use this:



Intent intent = new Intent(A, B.class);
A.startActivity(intent);


When I now navigate back from B to A I have a question:



1) When using Android's back button, the selected tab / scrolling position from A was remembered



2) When using an Intent or NavUtils.navigateUpFromSameTask(this); then the selected tab and scroll Position is NOT remembered but set to initial value



Can someone explain me what is going on here?










share|improve this question

























  • Hey @matthias, please accept an answer which worked for you!

    – Gourav
    Jan 1 at 14:48











  • Possible duplicate: stackoverflow.com/q/24487327/9819031

    – Gourav
    Jan 1 at 15:01
















2















I have two Activities A and B.



Activity A has a Tablayout with some Tabs. When I navigate from A to B I use this:



Intent intent = new Intent(A, B.class);
A.startActivity(intent);


When I now navigate back from B to A I have a question:



1) When using Android's back button, the selected tab / scrolling position from A was remembered



2) When using an Intent or NavUtils.navigateUpFromSameTask(this); then the selected tab and scroll Position is NOT remembered but set to initial value



Can someone explain me what is going on here?










share|improve this question

























  • Hey @matthias, please accept an answer which worked for you!

    – Gourav
    Jan 1 at 14:48











  • Possible duplicate: stackoverflow.com/q/24487327/9819031

    – Gourav
    Jan 1 at 15:01














2












2








2








I have two Activities A and B.



Activity A has a Tablayout with some Tabs. When I navigate from A to B I use this:



Intent intent = new Intent(A, B.class);
A.startActivity(intent);


When I now navigate back from B to A I have a question:



1) When using Android's back button, the selected tab / scrolling position from A was remembered



2) When using an Intent or NavUtils.navigateUpFromSameTask(this); then the selected tab and scroll Position is NOT remembered but set to initial value



Can someone explain me what is going on here?










share|improve this question
















I have two Activities A and B.



Activity A has a Tablayout with some Tabs. When I navigate from A to B I use this:



Intent intent = new Intent(A, B.class);
A.startActivity(intent);


When I now navigate back from B to A I have a question:



1) When using Android's back button, the selected tab / scrolling position from A was remembered



2) When using an Intent or NavUtils.navigateUpFromSameTask(this); then the selected tab and scroll Position is NOT remembered but set to initial value



Can someone explain me what is going on here?







android






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 1 at 18:08









Fantômas

32.8k156390




32.8k156390










asked Jan 1 at 14:26









matthiasmatthias

766727




766727













  • Hey @matthias, please accept an answer which worked for you!

    – Gourav
    Jan 1 at 14:48











  • Possible duplicate: stackoverflow.com/q/24487327/9819031

    – Gourav
    Jan 1 at 15:01



















  • Hey @matthias, please accept an answer which worked for you!

    – Gourav
    Jan 1 at 14:48











  • Possible duplicate: stackoverflow.com/q/24487327/9819031

    – Gourav
    Jan 1 at 15:01

















Hey @matthias, please accept an answer which worked for you!

– Gourav
Jan 1 at 14:48





Hey @matthias, please accept an answer which worked for you!

– Gourav
Jan 1 at 14:48













Possible duplicate: stackoverflow.com/q/24487327/9819031

– Gourav
Jan 1 at 15:01





Possible duplicate: stackoverflow.com/q/24487327/9819031

– Gourav
Jan 1 at 15:01












4 Answers
4






active

oldest

votes


















2














1) when navigation from activity A to B, the android system does not destroy activity A, but takes it to the back stack and adds B to the foreground. thats why when you press the back button or call onBackPressed() from the java code activity B is destroyed and A is set to the foreground. here is an example from the docs : Understand Tasks and Back stack
Android Activity Stack



2) when using an intent/navigateUpFromSameTask activity A is recreated and set to the foreground and B is set to the background, it's like adding another activity A to the stack so it will be A,B,A but if you press the back btn then you will be back to B and then A.



if you want to keep the scroll position and other data in activity A you call the onBackPressed in B or use the onSaveInstanceState to save the data and use it in the onCreate .



here is an example of saved instance:



@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putString("VariableName", variableData);
savedInstanceState.putString("VariableName", variableData);
savedInstanceState.putString("VariableName", variableData);
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.penguin_main);

if(savedInstanceState!=null){
bookData = (String) savedInstanceState.getSerializable("VariableName");
bookData = (String) savedInstanceState.getSerializable("VariableName");
bookData = (String) savedInstanceState.getSerializable("VariableName");
}
}





share|improve this answer


























  • Hey @medyas, it seems you are copying my and Balaji SB's code and using it combined!

    – Gourav
    Jan 1 at 14:47











  • i did not copy you answer or anyone's. he asked for explanation why did this happened and i answered.

    – medyas
    Jan 1 at 14:52











  • But do you agree that he does need only recommendation but not want to use any one! His question is a bit unclear. He wants just explanation!

    – Gourav
    Jan 1 at 14:53











  • give him some time to ready the answers and then he will reply or choose one

    – medyas
    Jan 1 at 14:55











  • ok let him take his time!

    – Gourav
    Jan 1 at 14:56



















1














You can set the current scroll position and tab position in activity A's on overriding onSaveInstance(Bundle savedInstanceState) method. When return to activity you can get onRestoreInstanceState(Bundle savedInstanceState) to restore it.
Hope it helps :)






share|improve this answer


























  • Hey @Balaji SB, this can become memory consuming! If Android provides a way to finish the activity without saving the data, then what's the problem? Sorry to say, but I disagree with you!

    – Gourav
    Jan 1 at 14:44











  • Hi @Gourav..Actually he don't need to finish the activity. He just navigate to some other activity and return to previous activity to maintain the state.

    – Balaji SB
    Jan 1 at 14:49











  • It seems @matthias just needs recommendation what is good or what's bad! His questions is a bit unclear on what he wants to do!

    – Gourav
    Jan 1 at 14:51



















1














Because NavUtils.navigateUpFromSameTask() just calls startActivity() and if android:launchMode="standard" the activity will be instantiated and created again and that is why can not remember the previous selected tab. To solve this issue you can override onNavigateUp() and inside that setCurrentItem(index) the index of tab you want to be displayed.



 @Override
public boolean onNavigateUp() {

myViewPager.setCurrentItem(position, true);
return true;
}


Edit
You can use another solution to solve the problem by setting android:launchMode="singleTop" on activity but this solution may not applicable in all the application.






share|improve this answer


























  • but where is the position element defined?

    – Gourav
    Jan 1 at 14:54











  • position is the index of your tab. you can put the index of the tab you want to be displayed when navigating from B to A

    – Amir
    Jan 1 at 14:56



















0














When you start an Activity, the first page will be opened! But when the back button is pressed, it navigates between the saved state of the activityTo simulate the back button pressed, you can try this:



@Override
public void onBackPressed() {
finish(); //your choice, thought not needed as super.onBackPressed(); is called if nothing is assigned here
}


or on toolbar back button clicked click:



toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});





share|improve this answer

























    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
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53996260%2fnavigating-back-to-previous-activity%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    4 Answers
    4






    active

    oldest

    votes








    4 Answers
    4






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    1) when navigation from activity A to B, the android system does not destroy activity A, but takes it to the back stack and adds B to the foreground. thats why when you press the back button or call onBackPressed() from the java code activity B is destroyed and A is set to the foreground. here is an example from the docs : Understand Tasks and Back stack
    Android Activity Stack



    2) when using an intent/navigateUpFromSameTask activity A is recreated and set to the foreground and B is set to the background, it's like adding another activity A to the stack so it will be A,B,A but if you press the back btn then you will be back to B and then A.



    if you want to keep the scroll position and other data in activity A you call the onBackPressed in B or use the onSaveInstanceState to save the data and use it in the onCreate .



    here is an example of saved instance:



    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);
    savedInstanceState.putString("VariableName", variableData);
    savedInstanceState.putString("VariableName", variableData);
    savedInstanceState.putString("VariableName", variableData);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.penguin_main);

    if(savedInstanceState!=null){
    bookData = (String) savedInstanceState.getSerializable("VariableName");
    bookData = (String) savedInstanceState.getSerializable("VariableName");
    bookData = (String) savedInstanceState.getSerializable("VariableName");
    }
    }





    share|improve this answer


























    • Hey @medyas, it seems you are copying my and Balaji SB's code and using it combined!

      – Gourav
      Jan 1 at 14:47











    • i did not copy you answer or anyone's. he asked for explanation why did this happened and i answered.

      – medyas
      Jan 1 at 14:52











    • But do you agree that he does need only recommendation but not want to use any one! His question is a bit unclear. He wants just explanation!

      – Gourav
      Jan 1 at 14:53











    • give him some time to ready the answers and then he will reply or choose one

      – medyas
      Jan 1 at 14:55











    • ok let him take his time!

      – Gourav
      Jan 1 at 14:56
















    2














    1) when navigation from activity A to B, the android system does not destroy activity A, but takes it to the back stack and adds B to the foreground. thats why when you press the back button or call onBackPressed() from the java code activity B is destroyed and A is set to the foreground. here is an example from the docs : Understand Tasks and Back stack
    Android Activity Stack



    2) when using an intent/navigateUpFromSameTask activity A is recreated and set to the foreground and B is set to the background, it's like adding another activity A to the stack so it will be A,B,A but if you press the back btn then you will be back to B and then A.



    if you want to keep the scroll position and other data in activity A you call the onBackPressed in B or use the onSaveInstanceState to save the data and use it in the onCreate .



    here is an example of saved instance:



    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);
    savedInstanceState.putString("VariableName", variableData);
    savedInstanceState.putString("VariableName", variableData);
    savedInstanceState.putString("VariableName", variableData);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.penguin_main);

    if(savedInstanceState!=null){
    bookData = (String) savedInstanceState.getSerializable("VariableName");
    bookData = (String) savedInstanceState.getSerializable("VariableName");
    bookData = (String) savedInstanceState.getSerializable("VariableName");
    }
    }





    share|improve this answer


























    • Hey @medyas, it seems you are copying my and Balaji SB's code and using it combined!

      – Gourav
      Jan 1 at 14:47











    • i did not copy you answer or anyone's. he asked for explanation why did this happened and i answered.

      – medyas
      Jan 1 at 14:52











    • But do you agree that he does need only recommendation but not want to use any one! His question is a bit unclear. He wants just explanation!

      – Gourav
      Jan 1 at 14:53











    • give him some time to ready the answers and then he will reply or choose one

      – medyas
      Jan 1 at 14:55











    • ok let him take his time!

      – Gourav
      Jan 1 at 14:56














    2












    2








    2







    1) when navigation from activity A to B, the android system does not destroy activity A, but takes it to the back stack and adds B to the foreground. thats why when you press the back button or call onBackPressed() from the java code activity B is destroyed and A is set to the foreground. here is an example from the docs : Understand Tasks and Back stack
    Android Activity Stack



    2) when using an intent/navigateUpFromSameTask activity A is recreated and set to the foreground and B is set to the background, it's like adding another activity A to the stack so it will be A,B,A but if you press the back btn then you will be back to B and then A.



    if you want to keep the scroll position and other data in activity A you call the onBackPressed in B or use the onSaveInstanceState to save the data and use it in the onCreate .



    here is an example of saved instance:



    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);
    savedInstanceState.putString("VariableName", variableData);
    savedInstanceState.putString("VariableName", variableData);
    savedInstanceState.putString("VariableName", variableData);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.penguin_main);

    if(savedInstanceState!=null){
    bookData = (String) savedInstanceState.getSerializable("VariableName");
    bookData = (String) savedInstanceState.getSerializable("VariableName");
    bookData = (String) savedInstanceState.getSerializable("VariableName");
    }
    }





    share|improve this answer















    1) when navigation from activity A to B, the android system does not destroy activity A, but takes it to the back stack and adds B to the foreground. thats why when you press the back button or call onBackPressed() from the java code activity B is destroyed and A is set to the foreground. here is an example from the docs : Understand Tasks and Back stack
    Android Activity Stack



    2) when using an intent/navigateUpFromSameTask activity A is recreated and set to the foreground and B is set to the background, it's like adding another activity A to the stack so it will be A,B,A but if you press the back btn then you will be back to B and then A.



    if you want to keep the scroll position and other data in activity A you call the onBackPressed in B or use the onSaveInstanceState to save the data and use it in the onCreate .



    here is an example of saved instance:



    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);
    savedInstanceState.putString("VariableName", variableData);
    savedInstanceState.putString("VariableName", variableData);
    savedInstanceState.putString("VariableName", variableData);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.penguin_main);

    if(savedInstanceState!=null){
    bookData = (String) savedInstanceState.getSerializable("VariableName");
    bookData = (String) savedInstanceState.getSerializable("VariableName");
    bookData = (String) savedInstanceState.getSerializable("VariableName");
    }
    }






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Jan 1 at 14:49

























    answered Jan 1 at 14:45









    medyasmedyas

    43038




    43038













    • Hey @medyas, it seems you are copying my and Balaji SB's code and using it combined!

      – Gourav
      Jan 1 at 14:47











    • i did not copy you answer or anyone's. he asked for explanation why did this happened and i answered.

      – medyas
      Jan 1 at 14:52











    • But do you agree that he does need only recommendation but not want to use any one! His question is a bit unclear. He wants just explanation!

      – Gourav
      Jan 1 at 14:53











    • give him some time to ready the answers and then he will reply or choose one

      – medyas
      Jan 1 at 14:55











    • ok let him take his time!

      – Gourav
      Jan 1 at 14:56



















    • Hey @medyas, it seems you are copying my and Balaji SB's code and using it combined!

      – Gourav
      Jan 1 at 14:47











    • i did not copy you answer or anyone's. he asked for explanation why did this happened and i answered.

      – medyas
      Jan 1 at 14:52











    • But do you agree that he does need only recommendation but not want to use any one! His question is a bit unclear. He wants just explanation!

      – Gourav
      Jan 1 at 14:53











    • give him some time to ready the answers and then he will reply or choose one

      – medyas
      Jan 1 at 14:55











    • ok let him take his time!

      – Gourav
      Jan 1 at 14:56

















    Hey @medyas, it seems you are copying my and Balaji SB's code and using it combined!

    – Gourav
    Jan 1 at 14:47





    Hey @medyas, it seems you are copying my and Balaji SB's code and using it combined!

    – Gourav
    Jan 1 at 14:47













    i did not copy you answer or anyone's. he asked for explanation why did this happened and i answered.

    – medyas
    Jan 1 at 14:52





    i did not copy you answer or anyone's. he asked for explanation why did this happened and i answered.

    – medyas
    Jan 1 at 14:52













    But do you agree that he does need only recommendation but not want to use any one! His question is a bit unclear. He wants just explanation!

    – Gourav
    Jan 1 at 14:53





    But do you agree that he does need only recommendation but not want to use any one! His question is a bit unclear. He wants just explanation!

    – Gourav
    Jan 1 at 14:53













    give him some time to ready the answers and then he will reply or choose one

    – medyas
    Jan 1 at 14:55





    give him some time to ready the answers and then he will reply or choose one

    – medyas
    Jan 1 at 14:55













    ok let him take his time!

    – Gourav
    Jan 1 at 14:56





    ok let him take his time!

    – Gourav
    Jan 1 at 14:56













    1














    You can set the current scroll position and tab position in activity A's on overriding onSaveInstance(Bundle savedInstanceState) method. When return to activity you can get onRestoreInstanceState(Bundle savedInstanceState) to restore it.
    Hope it helps :)






    share|improve this answer


























    • Hey @Balaji SB, this can become memory consuming! If Android provides a way to finish the activity without saving the data, then what's the problem? Sorry to say, but I disagree with you!

      – Gourav
      Jan 1 at 14:44











    • Hi @Gourav..Actually he don't need to finish the activity. He just navigate to some other activity and return to previous activity to maintain the state.

      – Balaji SB
      Jan 1 at 14:49











    • It seems @matthias just needs recommendation what is good or what's bad! His questions is a bit unclear on what he wants to do!

      – Gourav
      Jan 1 at 14:51
















    1














    You can set the current scroll position and tab position in activity A's on overriding onSaveInstance(Bundle savedInstanceState) method. When return to activity you can get onRestoreInstanceState(Bundle savedInstanceState) to restore it.
    Hope it helps :)






    share|improve this answer


























    • Hey @Balaji SB, this can become memory consuming! If Android provides a way to finish the activity without saving the data, then what's the problem? Sorry to say, but I disagree with you!

      – Gourav
      Jan 1 at 14:44











    • Hi @Gourav..Actually he don't need to finish the activity. He just navigate to some other activity and return to previous activity to maintain the state.

      – Balaji SB
      Jan 1 at 14:49











    • It seems @matthias just needs recommendation what is good or what's bad! His questions is a bit unclear on what he wants to do!

      – Gourav
      Jan 1 at 14:51














    1












    1








    1







    You can set the current scroll position and tab position in activity A's on overriding onSaveInstance(Bundle savedInstanceState) method. When return to activity you can get onRestoreInstanceState(Bundle savedInstanceState) to restore it.
    Hope it helps :)






    share|improve this answer















    You can set the current scroll position and tab position in activity A's on overriding onSaveInstance(Bundle savedInstanceState) method. When return to activity you can get onRestoreInstanceState(Bundle savedInstanceState) to restore it.
    Hope it helps :)







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Jan 1 at 14:50









    Gourav

    7391629




    7391629










    answered Jan 1 at 14:40









    Balaji SBBalaji SB

    216




    216













    • Hey @Balaji SB, this can become memory consuming! If Android provides a way to finish the activity without saving the data, then what's the problem? Sorry to say, but I disagree with you!

      – Gourav
      Jan 1 at 14:44











    • Hi @Gourav..Actually he don't need to finish the activity. He just navigate to some other activity and return to previous activity to maintain the state.

      – Balaji SB
      Jan 1 at 14:49











    • It seems @matthias just needs recommendation what is good or what's bad! His questions is a bit unclear on what he wants to do!

      – Gourav
      Jan 1 at 14:51



















    • Hey @Balaji SB, this can become memory consuming! If Android provides a way to finish the activity without saving the data, then what's the problem? Sorry to say, but I disagree with you!

      – Gourav
      Jan 1 at 14:44











    • Hi @Gourav..Actually he don't need to finish the activity. He just navigate to some other activity and return to previous activity to maintain the state.

      – Balaji SB
      Jan 1 at 14:49











    • It seems @matthias just needs recommendation what is good or what's bad! His questions is a bit unclear on what he wants to do!

      – Gourav
      Jan 1 at 14:51

















    Hey @Balaji SB, this can become memory consuming! If Android provides a way to finish the activity without saving the data, then what's the problem? Sorry to say, but I disagree with you!

    – Gourav
    Jan 1 at 14:44





    Hey @Balaji SB, this can become memory consuming! If Android provides a way to finish the activity without saving the data, then what's the problem? Sorry to say, but I disagree with you!

    – Gourav
    Jan 1 at 14:44













    Hi @Gourav..Actually he don't need to finish the activity. He just navigate to some other activity and return to previous activity to maintain the state.

    – Balaji SB
    Jan 1 at 14:49





    Hi @Gourav..Actually he don't need to finish the activity. He just navigate to some other activity and return to previous activity to maintain the state.

    – Balaji SB
    Jan 1 at 14:49













    It seems @matthias just needs recommendation what is good or what's bad! His questions is a bit unclear on what he wants to do!

    – Gourav
    Jan 1 at 14:51





    It seems @matthias just needs recommendation what is good or what's bad! His questions is a bit unclear on what he wants to do!

    – Gourav
    Jan 1 at 14:51











    1














    Because NavUtils.navigateUpFromSameTask() just calls startActivity() and if android:launchMode="standard" the activity will be instantiated and created again and that is why can not remember the previous selected tab. To solve this issue you can override onNavigateUp() and inside that setCurrentItem(index) the index of tab you want to be displayed.



     @Override
    public boolean onNavigateUp() {

    myViewPager.setCurrentItem(position, true);
    return true;
    }


    Edit
    You can use another solution to solve the problem by setting android:launchMode="singleTop" on activity but this solution may not applicable in all the application.






    share|improve this answer


























    • but where is the position element defined?

      – Gourav
      Jan 1 at 14:54











    • position is the index of your tab. you can put the index of the tab you want to be displayed when navigating from B to A

      – Amir
      Jan 1 at 14:56
















    1














    Because NavUtils.navigateUpFromSameTask() just calls startActivity() and if android:launchMode="standard" the activity will be instantiated and created again and that is why can not remember the previous selected tab. To solve this issue you can override onNavigateUp() and inside that setCurrentItem(index) the index of tab you want to be displayed.



     @Override
    public boolean onNavigateUp() {

    myViewPager.setCurrentItem(position, true);
    return true;
    }


    Edit
    You can use another solution to solve the problem by setting android:launchMode="singleTop" on activity but this solution may not applicable in all the application.






    share|improve this answer


























    • but where is the position element defined?

      – Gourav
      Jan 1 at 14:54











    • position is the index of your tab. you can put the index of the tab you want to be displayed when navigating from B to A

      – Amir
      Jan 1 at 14:56














    1












    1








    1







    Because NavUtils.navigateUpFromSameTask() just calls startActivity() and if android:launchMode="standard" the activity will be instantiated and created again and that is why can not remember the previous selected tab. To solve this issue you can override onNavigateUp() and inside that setCurrentItem(index) the index of tab you want to be displayed.



     @Override
    public boolean onNavigateUp() {

    myViewPager.setCurrentItem(position, true);
    return true;
    }


    Edit
    You can use another solution to solve the problem by setting android:launchMode="singleTop" on activity but this solution may not applicable in all the application.






    share|improve this answer















    Because NavUtils.navigateUpFromSameTask() just calls startActivity() and if android:launchMode="standard" the activity will be instantiated and created again and that is why can not remember the previous selected tab. To solve this issue you can override onNavigateUp() and inside that setCurrentItem(index) the index of tab you want to be displayed.



     @Override
    public boolean onNavigateUp() {

    myViewPager.setCurrentItem(position, true);
    return true;
    }


    Edit
    You can use another solution to solve the problem by setting android:launchMode="singleTop" on activity but this solution may not applicable in all the application.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Jan 1 at 15:05

























    answered Jan 1 at 14:52









    AmirAmir

    161214




    161214













    • but where is the position element defined?

      – Gourav
      Jan 1 at 14:54











    • position is the index of your tab. you can put the index of the tab you want to be displayed when navigating from B to A

      – Amir
      Jan 1 at 14:56



















    • but where is the position element defined?

      – Gourav
      Jan 1 at 14:54











    • position is the index of your tab. you can put the index of the tab you want to be displayed when navigating from B to A

      – Amir
      Jan 1 at 14:56

















    but where is the position element defined?

    – Gourav
    Jan 1 at 14:54





    but where is the position element defined?

    – Gourav
    Jan 1 at 14:54













    position is the index of your tab. you can put the index of the tab you want to be displayed when navigating from B to A

    – Amir
    Jan 1 at 14:56





    position is the index of your tab. you can put the index of the tab you want to be displayed when navigating from B to A

    – Amir
    Jan 1 at 14:56











    0














    When you start an Activity, the first page will be opened! But when the back button is pressed, it navigates between the saved state of the activityTo simulate the back button pressed, you can try this:



    @Override
    public void onBackPressed() {
    finish(); //your choice, thought not needed as super.onBackPressed(); is called if nothing is assigned here
    }


    or on toolbar back button clicked click:



    toolbar.setNavigationOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    finish();
    }
    });





    share|improve this answer






























      0














      When you start an Activity, the first page will be opened! But when the back button is pressed, it navigates between the saved state of the activityTo simulate the back button pressed, you can try this:



      @Override
      public void onBackPressed() {
      finish(); //your choice, thought not needed as super.onBackPressed(); is called if nothing is assigned here
      }


      or on toolbar back button clicked click:



      toolbar.setNavigationOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
      finish();
      }
      });





      share|improve this answer




























        0












        0








        0







        When you start an Activity, the first page will be opened! But when the back button is pressed, it navigates between the saved state of the activityTo simulate the back button pressed, you can try this:



        @Override
        public void onBackPressed() {
        finish(); //your choice, thought not needed as super.onBackPressed(); is called if nothing is assigned here
        }


        or on toolbar back button clicked click:



        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        finish();
        }
        });





        share|improve this answer















        When you start an Activity, the first page will be opened! But when the back button is pressed, it navigates between the saved state of the activityTo simulate the back button pressed, you can try this:



        @Override
        public void onBackPressed() {
        finish(); //your choice, thought not needed as super.onBackPressed(); is called if nothing is assigned here
        }


        or on toolbar back button clicked click:



        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        finish();
        }
        });






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Jan 1 at 21:33









        halfer

        14.7k758115




        14.7k758115










        answered Jan 1 at 14:34









        GouravGourav

        7391629




        7391629






























            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53996260%2fnavigating-back-to-previous-activity%23new-answer', 'question_page');
            }
            );

            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







            Popular posts from this blog

            MongoDB - Not Authorized To Execute Command

            How to fix TextFormField cause rebuild widget in Flutter

            in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith