1

I have a custom Back button included in my Android Application. My Custom animation when clicking the fragments is ok but it seems I can't apply animation when clicking my own back button or even the back button of android. I don't know how to fix it, I already included these code in my Activity Class but it seems not to work.

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {
        case R.id.menuitem1:
            break;
        case R.id.menuitem2:
            super.onBackPressed();
            overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
            break;

        default:
            break;
        }

        return true;
    }

I also tried to override the onbackPressed method but it seems also not to work

   @Override
    public void onBackPressed() {
        super.onBackPressed();

        overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);

    }

I don't know how to implement this one...Kindly help me wth this one because we are running out of time because of this. Thank you

Waiting for positive responses.

My Android Application Screencast http://content.screencast.com/users/junnel1001/folders/Jing/media/2a13fd01-f467-4889-ac21-743c188a0f67/2012-07-09_1032.png

EDIT

ok this is how I set up my Fragments

In my main Activity:

 public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);


    setContentView(R.layout.main);


    ActionBar actionBar = getActionBar();
    Drawable myIcon = getResources().getDrawable( R.drawable.header_bg );
    actionBar.setSplitBackgroundDrawable(myIcon);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    ActionBar.Tab tabA = actionBar.newTab().setIcon(R.drawable.about);
    ActionBar.Tab tabB = actionBar.newTab().setIcon(R.drawable.sponsors);
    ActionBar.Tab tabC = actionBar.newTab().setIcon(R.drawable.map);
    ActionBar.Tab tabD = actionBar.newTab().setIcon(R.drawable.destination);
    Fragment aboutTab = new PhotosActivity();
    Fragment sponsorTab = new SongsActivity();
    Fragment mapTab = new HCCMapActivity(); 
    Fragment DestinationTab = new DestinationActivity();
    tabA.setTabListener(new MyTabsListener(aboutTab));
    tabB.setTabListener(new MyTabsListener(sponsorTab));
    tabC.setTabListener(new MyTabsListener(mapTab));
    tabD.setTabListener(new MyTabsListener(DestinationTab));
    actionBar.addTab(tabD, 0, true);
    actionBar.addTab(tabC, 1, false);
    actionBar.addTab(tabA, 2, false);
    actionBar.addTab(tabB, 3, false);

}

protected class MyTabsListener implements TabListener{

        Fragment fragment;

        public MyTabsListener(Fragment fragment){

            this.fragment = fragment;
        }

        public void onTabSelected(Tab tab, FragmentTransaction ft) {           
            Log.i("Tab Position Now", Integer.toString( tab.getPosition()));
            if (myTabPosition < 0){
                ft.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out);    
            }else{
                if (myTabPosition >  tab.getPosition()){
                    ft.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right);   
                    Log.i("Tab Position Before",Integer.toString( myTabPosition));
                }else{
                    ft.setCustomAnimations(R.anim.slide_out_left, R.anim.slide_out_right);      
                    Log.i("Tab Position Before1",Integer.toString( myTabPosition));
                }
            }   
            myTabPosition = tab.getPosition();
            ft.replace(R.id.fragment_place, fragment, null);    
        //  ft.commit();
        }

        public void onTabUnselected(Tab tab, FragmentTransaction ft) {
            // TODO Auto-generated method stub
            ft.remove(fragment);

        }

        public void onTabReselected(Tab tab, FragmentTransaction ft) {
            // TODO Auto-generated method stub

        }





    }

In one of my fragments Class:

E.g destinationActivity

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
             View view = inflater.inflate(R.layout.destination_layout, container, false);                                
             ArrayList<Category> category = new ArrayList<Category>();
             ListView lv = (ListView)view.findViewById(android.R.id.list);
             category.add(new Category("Image", "Image Banner"));
             category.add(new Category("HISTORICAL TOURS", "Historical Desc"));
             category.add(new Category("CULTURAL TOURS", "Cultural Desc"));
             lv.setAdapter(new CategoryAdapter(getActivity(), R.layout.list_view, category));
             lv.setClickable(true);
             super.setMenuVisibility (false);
             super.setHasOptionsMenu(false);
             return view;
     } 


     @Override
     public void onListItemClick(ListView l, View v, int position, long id) {
         Log.i("FragmentList", "Item clicked: " + id);
         final FragmentTransaction ft = getFragmentManager().beginTransaction(); 
         if (id == 1){
        //   ft.remove(fragment)
             ft.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_in_right);   
             ft.replace(R.id.fragment_place, new ListLocationActivity(), null); 
             ft.addToBackStack(null);
             ft.commit(); 
         }else if (id == 2){
             ft.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_in_right);   
             ft.replace(R.id.fragment_place, new ListLocationActivity(), null); 
             ft.addToBackStack(null);
             ft.commit(); 
         }

     }
Junnel Gallemaso
  • 861
  • 2
  • 9
  • 23
  • Is adding a second back button needed? That sort of thing is discouraged in the Android Design Pattern doco ( http://developer.android.com/design/patterns/index.html ). This doesn't really fix your underlying problem however. Can you shed some light on how you have your fragments set up? – Ben Neill Jul 09 '12 at 02:48
  • Ok...even if there is no custom back button in my android Application..Animation doesn't work either to the android's Back button (in virtual navigation controls)...Please reply...Thanks – Junnel Gallemaso Jul 09 '12 at 03:56
  • There was a bug in pre ICS compat library that suffered this problem - http://code.google.com/p/android/issues/detail?id=15623&colspec=ID%20Type%20Status%20Owner%20Summary%20Stars - have you updated to the latest compat libraries? Nothing I can see looks wrong to me at first glance... – Ben Neill Jul 09 '12 at 04:47
  • Possible duplicate of [Android - How to animate an activity transition when the default back button is pressed](http://stackoverflow.com/questions/12047770/android-how-to-animate-an-activity-transition-when-the-default-back-button-is) – tir38 Jul 13 '16 at 21:27

1 Answers1

1

Do you really need to have it override the backbutton functionality? Why don't you just create a new event trigger so that it has its own functionality rather than an override? So when "this button is pressed" "slide in/out". Then if you want to finish an activity you can just put in a "finish()" command or "finishActivity()"

Android: Go back to previous activity

"If you need to go back to previous activities using the back button, you will not have animation transitions using overridePendingTransition(). For activities which only occur once and are then removed from the history stack, such as splash activities, overridePendingTransition() is perfect. If you need animations back to your previous activities, you might consider other alternatives."

http://www.anddev.org/novice-tutorials-f8/splash-fade-activity-animations-overridependingtransition-t9464.html

Community
  • 1
  • 1
sksallaj
  • 3,872
  • 3
  • 37
  • 58
  • I just want to implement a custom animation for the back button...but I don't know how to do it....for example..if i want to create own functionality of this button...how am i going to implement it also to android's built-in back button?? – Junnel Gallemaso Jul 09 '12 at 04:22
  • Just a curious question, when you tried your bottom code of method override, did you change your case statement to call your version of onBackPressed ? I never really seen anyone put in a "super" keyword in a case statement since it's used to override a method with the same name. – sksallaj Jul 09 '12 at 04:50