0

I'm not able to figure out this problem. I'm using ActionBarSherlock in my application which consists of two fragments inside the main activity.Each fragment consists of a list populated from database. Everything else is working fine. There seems to be a problem with the back button. Ideally it should terminate the application but in my case when I press the back button from within a fragment,instead of exiting the application it shows the fragment with an empty list and I have to press the back button again to terminate the application. I read this post but am not able to figure out how to resolve this problem. Fragment: which callback invoked when press back button & customize it Please help!!

protected class MyTabListener implements ActionBar.TabListener
    {

    Fragment fragment;
    public MyTabListener( Fragment fragment) {
        // TODO Auto-generated constructor stub

        this.fragment=fragment;
    }

    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub
        ft.replace(R.id.fragment_place,fragment,null);
    }

    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

    }

}
Community
  • 1
  • 1
user1951690
  • 167
  • 1
  • 4
  • 9
  • Are you using addToBackstack() function? Can you post code regarding the FragmentTransactions? – Tobrun Jan 14 '13 at 21:37
  • No, relatively new to android. Not using FragmentTransactions. – user1951690 Jan 14 '13 at 21:39
  • How are you adding the fragments to the activity? are you using static fragments (created from xml)? without code it will be hard to find the problem – Tobrun Jan 14 '13 at 21:40
  • I just want to terminate the application when I press the back button from within the fragment but I cannot override the onBackPressed() in a fragment. Is there a way to exit the application from within the fragment? that's what I want to know?? – user1951690 Jan 14 '13 at 21:43
  • onBackPressed event is handled inside an activity, you can finish that activity from there. You can also close the application from within the fragment by calling getActivity().finish(). (But its better to find a good implementation of fragments then using these 'hacks' to get the required functionality) I will post a link to a tutorial regarding actionbartabs and fragments – Tobrun Jan 14 '13 at 21:46
  • I tried doing that. I called getSherockActivity.finish() but it didn't work. – user1951690 Jan 14 '13 at 21:51
  • Instead of trying to finish the "wrong" way, try to implement the tutorial below. Then fragments will behave as normal when back is pressed – Tobrun Jan 14 '13 at 21:51
  • If it works it would be helpfull if you accepted the answer ;) good luck coding ;) – Tobrun Jan 14 '13 at 21:57

2 Answers2

1

You could implement the onKeyDown method directly from your main activity (in which you define the fragments associated to each tab or panel), for instance:

public class main_pager extends SherlockFragmentActivity{

...

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {

if (keyCode == KeyEvent.KEYCODE_BACK) {

finish();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}
cdesir
  • 139
  • 5
0

Thanks for the reply! Setting android:noHistory="true" in the manifest solved the issue.

user1951690
  • 167
  • 1
  • 4
  • 9