Issue with back button between Fragments (backstack) - android

I have an app with 3 tabs. For two of theese tabs I have buttons where I change the current fragment with a new one with this code:
MapFragment newFragment = new JourneyMapFragment(mContext, getFromDestinationCoordinate(), getToDestinationCoordinate());
android.app.FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.setCustomAnimations(android.R.animator.fade_in,
android.R.animator.fade_out);
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
if(newFragment.isHidden()){
transaction.show(newFragment);
}
transaction.commit();
For on of the tabs which is a normal fragment, changing into a map fragment the back button takes me back to the original fragment with no issuse.
However, another tab which is a mapfragment, changing into a normal fragment does not give me the same action when pressing the back button. When it's pressed it changes the view to a white/black view.
This is the transaction code within the tab where the back button won't work:
Fragment newFragment = new CloseBusStopFragment(mContext, busStopList, getMyPosition());
android.app.FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.setCustomAnimations(android.R.animator.fade_in,
android.R.animator.fade_out);
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
if(newFragment.isHidden()){
transaction.show(newFragment);
}
// Commit the transaction
transaction.commit();
Anyone know why this happens? Any help?

If you can do that manually, you can try overriding the onBackPressed() and do whatever you want manually.
#Override
public void onBackPressed() {
// Do whatever your transaction manually
super.onBackPressed();
}

Related

Replace Fragment and press back

Hi Following my code to show detail fragment from list fragment
getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.FragmentContainer1, DealFragment.newInstance(dealItems, position, currentPage, totalCount)).addToBackStack(null).commit();
Now when I press back button I get new ListFragment.ListFragmnt state is not saved.
I referred to some stack questions but haven't got right answer
I tried below code but it causes issues when app goes in background and is killed by system(Like I am opening chrome from my detail view and when I go back from chrome my app is closed and minimised) FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
ft.hide(getActivity().getSupportFragmentManager().findFragmentById(containerId));
ft.add(containerId, detailFragment);
ft.addToBackStack(null);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.commit();
Any solution on this problem when I move to ListFragment to detail hot maintain state of detail fragment.
I have referred this link from stack overflow here is the link
I want functionality same as Gmail app when we go from list to detail and come back to list fragment. Scroll position and everything is maintained which is not happening in my case
Try this :
#Override
public void onBackPressed() {
if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
getSupportFragmentManager().popBackStack();
} else {
this.finish();
}
}
In your activity set the first fragment:
ListFragment listFragment = new ListFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.add(R.id.FragmentContainer1, listFragment );
transaction.addToBackStack(null);
transaction.commit();
Then in listfragment call details fragment:
DetailFragment detailFragment = new DetailFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.FragmentContainer1, detailFragment);
transaction.addToBackStack(null);
transaction.commit();

Issue in Fragments in android

When I click on the back icon in the layout then it goes to previous fragment but it doesn't go to the fragment it was killed. what is the solution for this?
I'm using finish() and backstack but it not works for me
back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
android.support.v4.app.Fragment onlineFragments = new OnlineFragments();
android.support.v4.app.FragmentManager fragmentManagerprofile = getActivity().getSupportFragmentManager();
android.support.v4.app.FragmentTransaction fragmentprofileTransaction = fragmentManagerprofile.beginTransaction();
fragmentprofileTransaction.replace(R.id.background_fragment, onlineFragments);
fragmentprofileTransaction.commit();
}
});
Fragment A
case R.id.recharge:
HomeActvity.toolbar.setVisibility(View.GONE);
android.support.v4.app.Fragment Recharge = new Prepaid_recharge();
android.support.v4.app.FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.containerView, Recharge);
fragmentTransaction.commit();
break;
Whenever your making Transactions between fragments and you want to navigate back to the previous fragment(s) (Back Button), in the transaction, you must add this transaction to the backStack before committing:
Android docs:
"Before you call commit(), however, you might want to call addToBackStack(), in order to add the transaction to a back stack of fragment transactions. This back stack is managed by the activity and allows the user to return to the previous fragment state, by pressing the Back button."
https://developer.android.com/guide/components/fragments.html#Transactions
// Create new fragment and transaction
Fragment newFragment = new ExampleFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
I use this code to add and remove the fragment. In case I do not need that fragment to be in the back stack I send the boolean value as false. Doing this when pressing back button from toolbar It open the correct activity
public static void changeFragment (MyActivity activity, Fragment fragment, int fragmentContainer, boolean addToBackStack){
FragmentManager fragmentManager = activity.getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(fragmentContainer, fragment);
/*here we keep track of fragments and avoiding last blank fragment by passing true*/
if(addToBackStack){
fragmentTransaction.addToBackStack(null);
}
fragmentTransaction.commit();
}

Why I got onBackPressed issue in Android

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
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.
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!

Android change Fragment when close ActivityDialog

I want to change Fragment when I close the activity. I tried to enter into the activity, the FragmentTransaction but it is not the correct way. How can I do? Thank you
Fragment newFragment = new Ele_Fragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.frame_container, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
try adding a tag to the addToBackStack instead of passing null
.addToBackStack(Ele_Fragment.TAG);
this way you have a hook into the fragments you are passing.

Refresh a fragment from Activity using hide/show or detach/attach

I'm trying to refresh the content of 2 fragments in my activity after an update done in my DB.
I saw some answers telling to use hide/show or attach/detach but it doesn't work for me...maye be I'm doing something wrong...
Thanks for your help !
// Create fragment and give it an argument specifying the article it should show
ArticleFragment newFragment = new ArticleFragment();
Bundle args = new Bundle();
args.putInt(ArticleFragment.ARG_POSITION, position);
newFragment.setArguments(args);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack so the user can navigate back
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
taken from here: https://developer.android.com/training/basics/fragments/fragment-ui.html

Categories

Resources