1

when I click on the back press on real device it went to the home page not to the previous page ,here I am Using fragments how to solve that issue

In First Fragment

NotesFragment notes = new NotesFragment();
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.day_fragment_mainLayout, notes);
transaction.addToBackStack(null);
transaction.commit();

in Second Fragment

DayFragment day = new DayFragment();
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.addToBackStack(null);
transaction.replace(R.id.day_fragment_mainLayout, day);
transaction.commit();

in Third Fragment

ItemsFragment items = new ItemsFragment();  
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.addToBackStack(null);
transaction.replace(R.id.day_fragment_mainLayout, items);
transaction.commit();

when i click on backpress button its goes to the home page,bt i need prevoius page.

note: btnclick i am using to navigating fragments one to one

Gueorgui Obregon
  • 5,077
  • 3
  • 33
  • 57
Manikanta Reddy
  • 631
  • 9
  • 15
  • Please go through this link http://stackoverflow.com/questions/27717127/how-to-handle-backpress-with-fragment/27717201#27717201 – sandeepmaaram Feb 02 '15 at 09:49
  • your first fragment is replaced by second fragment and second fragment is replaced by third fragment.. And when you are on third fragment and click on back button, you are navigated to home page instead of second fragment.. is that correct? – iMDroid Feb 02 '15 at 10:15

2 Answers2

1

You could achieve it by using FragmentTransaction's add() method, and then override onBackPressed where you have to pop your FragmentManager's back stack. This will result in the behaviour you described.

Zsolt Boldizsar
  • 2,447
  • 2
  • 27
  • 37
0

You cannot go back because you didn't open any new activity. OnBackPressed is moving you to the previous activity stored in the stack. If you want to go back to the previous fragment, you have to store the previous fragment somewhere and then use:

@Override
public void onBackPressed() {
// here you should change the fragment
    transaction.replace(YOUR_PREVIOUS_FRAGMENT, items);
    transaction.commit();
}

Thanks for attention!

Vladislav Kan
  • 354
  • 1
  • 12