2

I'm using this tutorial to implement facebook login etc.

Facebook Login

I have added new fragment in this to show list of friends. Now when I press back button on my newly added fragment it takes me to the SPLASH fragment, I want same behaviour on back button on action bar. Means when I'm on my new fragment it shows me a back button on action bar. And pressing that back button takes me back to the SPLASH screen.

enter image description here

private void showFragment(int fragmentIndex, boolean addToBackStack) {
    FragmentManager fm = getSupportFragmentManager();
    FragmentTransaction transaction = fm.beginTransaction();
    for (int i = 0; i < fragments.length; i++) {
        if (i == fragmentIndex) {
            transaction.show(fragments[i]);
        } else {
            transaction.hide(fragments[i]);
        }
    }
    if (addToBackStack) {
        transaction.addToBackStack(null);
    }
    transaction.commit();
}
Yawar
  • 1,886
  • 3
  • 29
  • 38
  • possible duplicate of [how to back to previous fragment on pressing manually back button of indivisual fragment?](http://stackoverflow.com/questions/14275627/how-to-back-to-previous-fragment-on-pressing-manually-back-button-of-indivisual) – Maveňツ Aug 06 '14 at 06:17
  • 1
    Saw that already but no answered is selected as answer there, so confused – Yawar Aug 06 '14 at 06:23

2 Answers2

4

You can do that by two ways :

1. Inside your fragment

    @Override
    public void onDetach()
    {
    super.onDetach();
    PUT YOUR CODE HERE    
    }

This will called when fragment will be finished.

2. Just add addToBackStack while you are transitioning between your fragments like below:

    fragmentManager.beginTransaction().replace(R.id.content_frame,fragment).addToBackStack("tag").commit();

if you write addToBackStack(null) , it will handle it by itself but if you give a tag , you should handle it manually.

EDITED:

for doing transactions

FragmentTransaction fragmentTransaction =  getActivity().getSupportFragmentManager().beginTransaction();         
Fragment scheduleFragment = new ScheduleFragment();     
fragmentTransaction.replace(R.id.content_container, scheduleFragment, "scheduleFragment");
fragmentTransaction.addToBackStack("scheduleFragment");
fragmentTransaction.commit();

@Yawar actionbar is on activity only and this will added on activity it will be called evertime when u press actionbar home button-->

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
    case android.R.id.home:
        // app icon in action bar clicked; goto parent activity.
        this.finish();
        return true;
    default:
        return super.onOptionsItemSelected(item);
}
}
Maveňツ
  • 9,147
  • 14
  • 53
  • 94
  • I used to do by the second method :) – Maveňツ Aug 06 '14 at 06:39
  • Sir I have edited my question with a function code snippet which I'm using to transfer from on fragment to another very efficiently. What I want is to add a back button on top of my fragment 4 which takes me back to Fragment 3. – Yawar Aug 06 '14 at 06:42
  • 1
    My application already does it with out back button on action bar. Mean when pressing back button of android device on fragment 4 it takes me to fragment 3. I want a button also at top of fragment 4 to do the same for me. – Yawar Aug 06 '14 at 06:43
  • then make a transation on button click – Maveňツ Aug 06 '14 at 06:47
  • I don't know how to add back button on action bar. – Yawar Aug 06 '14 at 06:48
  • DO u want to add back button on actionBar? – Maveňツ Aug 06 '14 at 06:52
  • Yes, with same functionality as default back button in android. – Yawar Aug 06 '14 at 06:52
  • then check this link http://stackoverflow.com/questions/12070744/add-back-button-to-action-bar – Maveňツ Aug 06 '14 at 06:56
  • This and many questions like this in stackoverflow are for activities. I have to do this for fragments. These examples are not working for me. – Yawar Aug 06 '14 at 06:59
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/58751/discussion-between-mann-and-yawar). – Maveňツ Aug 06 '14 at 07:06
-1

i got this code on stackoverflow after searching hard hope this may help you

ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);// in on Create()

search for code onOptionsItemSelected(MenuItem item) and edit it in this way

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
    case android.R.id.home:
        // change your behaviour here
        Intent intent = new Intent(this, yourclass.class);// i started new activity here
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
        return true;
  }
    return super.onOptionsItemSelected(item);
}
jaimin
  • 563
  • 7
  • 25