Activity slide animation only works when back button is pressed












0















Im trying to add a sliding animation when entering and exiting an activity but it only works when the phones back button is pressed. When pressing the up button in the toolbar or a separate button it doesn't work.



I tried adding the overridePendingTransition(R.anim.no_anim, R.anim.slide_out_right); line to the onPause() method as well as the finish() method and the buttons OnClick method but it doesn't work :/



no_anim.xml:



<?xml version="1.0" encoding="utf-8"?>

<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="0"
android:fromXDelta="0"
android:toXDelta="0" />
</set>


slide_out_right.xml:



<?xml version="1.0" encoding="utf-8"?>

<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="@android:integer/config_longAnimTime"
android:fromXDelta="0"
android:toXDelta="100%p" />
</set>


Java:



public void button(View view) {
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
overridePendingTransition(R.anim.no_anim, R.anim.slide_out_right);
}

@Override
public void finish(){
super.finish();
overridePendingTransition(R.anim.no_anim, R.anim.slide_out_right);
}

@Override
protected void onPause() {
super.onPause();
overridePendingTransition(R.anim.no_anim, R.anim.slide_out_right);
}









share|improve this question























  • Use toolbar.setNavigationOnClickListener(); as suggested in my answer!

    – Gourav
    Jan 1 at 15:53
















0















Im trying to add a sliding animation when entering and exiting an activity but it only works when the phones back button is pressed. When pressing the up button in the toolbar or a separate button it doesn't work.



I tried adding the overridePendingTransition(R.anim.no_anim, R.anim.slide_out_right); line to the onPause() method as well as the finish() method and the buttons OnClick method but it doesn't work :/



no_anim.xml:



<?xml version="1.0" encoding="utf-8"?>

<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="0"
android:fromXDelta="0"
android:toXDelta="0" />
</set>


slide_out_right.xml:



<?xml version="1.0" encoding="utf-8"?>

<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="@android:integer/config_longAnimTime"
android:fromXDelta="0"
android:toXDelta="100%p" />
</set>


Java:



public void button(View view) {
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
overridePendingTransition(R.anim.no_anim, R.anim.slide_out_right);
}

@Override
public void finish(){
super.finish();
overridePendingTransition(R.anim.no_anim, R.anim.slide_out_right);
}

@Override
protected void onPause() {
super.onPause();
overridePendingTransition(R.anim.no_anim, R.anim.slide_out_right);
}









share|improve this question























  • Use toolbar.setNavigationOnClickListener(); as suggested in my answer!

    – Gourav
    Jan 1 at 15:53














0












0








0








Im trying to add a sliding animation when entering and exiting an activity but it only works when the phones back button is pressed. When pressing the up button in the toolbar or a separate button it doesn't work.



I tried adding the overridePendingTransition(R.anim.no_anim, R.anim.slide_out_right); line to the onPause() method as well as the finish() method and the buttons OnClick method but it doesn't work :/



no_anim.xml:



<?xml version="1.0" encoding="utf-8"?>

<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="0"
android:fromXDelta="0"
android:toXDelta="0" />
</set>


slide_out_right.xml:



<?xml version="1.0" encoding="utf-8"?>

<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="@android:integer/config_longAnimTime"
android:fromXDelta="0"
android:toXDelta="100%p" />
</set>


Java:



public void button(View view) {
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
overridePendingTransition(R.anim.no_anim, R.anim.slide_out_right);
}

@Override
public void finish(){
super.finish();
overridePendingTransition(R.anim.no_anim, R.anim.slide_out_right);
}

@Override
protected void onPause() {
super.onPause();
overridePendingTransition(R.anim.no_anim, R.anim.slide_out_right);
}









share|improve this question














Im trying to add a sliding animation when entering and exiting an activity but it only works when the phones back button is pressed. When pressing the up button in the toolbar or a separate button it doesn't work.



I tried adding the overridePendingTransition(R.anim.no_anim, R.anim.slide_out_right); line to the onPause() method as well as the finish() method and the buttons OnClick method but it doesn't work :/



no_anim.xml:



<?xml version="1.0" encoding="utf-8"?>

<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="0"
android:fromXDelta="0"
android:toXDelta="0" />
</set>


slide_out_right.xml:



<?xml version="1.0" encoding="utf-8"?>

<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="@android:integer/config_longAnimTime"
android:fromXDelta="0"
android:toXDelta="100%p" />
</set>


Java:



public void button(View view) {
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
overridePendingTransition(R.anim.no_anim, R.anim.slide_out_right);
}

@Override
public void finish(){
super.finish();
overridePendingTransition(R.anim.no_anim, R.anim.slide_out_right);
}

@Override
protected void onPause() {
super.onPause();
overridePendingTransition(R.anim.no_anim, R.anim.slide_out_right);
}






android animation overridependingtransition






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 1 at 14:56









Pepe SilviaPepe Silvia

4116




4116













  • Use toolbar.setNavigationOnClickListener(); as suggested in my answer!

    – Gourav
    Jan 1 at 15:53



















  • Use toolbar.setNavigationOnClickListener(); as suggested in my answer!

    – Gourav
    Jan 1 at 15:53

















Use toolbar.setNavigationOnClickListener(); as suggested in my answer!

– Gourav
Jan 1 at 15:53





Use toolbar.setNavigationOnClickListener(); as suggested in my answer!

– Gourav
Jan 1 at 15:53












2 Answers
2






active

oldest

votes


















1














You can call onBackPressed() when pressing the up button in the toolbar






share|improve this answer
























  • This is not regarded as a good approach! onBackPressed() should be called when we want to handle default back button clicks in android! If android wished to do so, why they would have added the facility to provide back click facility on toolbar with its toolbar.setNavigationOnClickListener()?

    – Gourav
    Jan 1 at 15:55



















0














Set toolbar Navigation click listener:



toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish(); //close activity
overridePendingTransition(R.anim.your_anim, R.anim.your_anim);
}
});


This will surely work!






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%2f53996461%2factivity-slide-animation-only-works-when-back-button-is-pressed%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    You can call onBackPressed() when pressing the up button in the toolbar






    share|improve this answer
























    • This is not regarded as a good approach! onBackPressed() should be called when we want to handle default back button clicks in android! If android wished to do so, why they would have added the facility to provide back click facility on toolbar with its toolbar.setNavigationOnClickListener()?

      – Gourav
      Jan 1 at 15:55
















    1














    You can call onBackPressed() when pressing the up button in the toolbar






    share|improve this answer
























    • This is not regarded as a good approach! onBackPressed() should be called when we want to handle default back button clicks in android! If android wished to do so, why they would have added the facility to provide back click facility on toolbar with its toolbar.setNavigationOnClickListener()?

      – Gourav
      Jan 1 at 15:55














    1












    1








    1







    You can call onBackPressed() when pressing the up button in the toolbar






    share|improve this answer













    You can call onBackPressed() when pressing the up button in the toolbar







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Jan 1 at 15:02









    Saurav GhimireSaurav Ghimire

    341113




    341113













    • This is not regarded as a good approach! onBackPressed() should be called when we want to handle default back button clicks in android! If android wished to do so, why they would have added the facility to provide back click facility on toolbar with its toolbar.setNavigationOnClickListener()?

      – Gourav
      Jan 1 at 15:55



















    • This is not regarded as a good approach! onBackPressed() should be called when we want to handle default back button clicks in android! If android wished to do so, why they would have added the facility to provide back click facility on toolbar with its toolbar.setNavigationOnClickListener()?

      – Gourav
      Jan 1 at 15:55

















    This is not regarded as a good approach! onBackPressed() should be called when we want to handle default back button clicks in android! If android wished to do so, why they would have added the facility to provide back click facility on toolbar with its toolbar.setNavigationOnClickListener()?

    – Gourav
    Jan 1 at 15:55





    This is not regarded as a good approach! onBackPressed() should be called when we want to handle default back button clicks in android! If android wished to do so, why they would have added the facility to provide back click facility on toolbar with its toolbar.setNavigationOnClickListener()?

    – Gourav
    Jan 1 at 15:55













    0














    Set toolbar Navigation click listener:



    toolbar.setNavigationOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    finish(); //close activity
    overridePendingTransition(R.anim.your_anim, R.anim.your_anim);
    }
    });


    This will surely work!






    share|improve this answer




























      0














      Set toolbar Navigation click listener:



      toolbar.setNavigationOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
      finish(); //close activity
      overridePendingTransition(R.anim.your_anim, R.anim.your_anim);
      }
      });


      This will surely work!






      share|improve this answer


























        0












        0








        0







        Set toolbar Navigation click listener:



        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        finish(); //close activity
        overridePendingTransition(R.anim.your_anim, R.anim.your_anim);
        }
        });


        This will surely work!






        share|improve this answer













        Set toolbar Navigation click listener:



        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        finish(); //close activity
        overridePendingTransition(R.anim.your_anim, R.anim.your_anim);
        }
        });


        This will surely work!







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 1 at 15:47









        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%2f53996461%2factivity-slide-animation-only-works-when-back-button-is-pressed%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

            'app-layout' is not a known element: how to share Component with different Modules

            android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

            WPF add header to Image with URL pettitions [duplicate]