1

Let us consider that i have 2 fragment a & b

Fragment a contains view pager, horizontal grid view and buttons images etc with scroll. When i click a item in Fragment a it goes to fragment b and when i click on back button in fragment b it comes back to fragment a. Is all working perfect.

My issue is in back press of the fragment b , it load the fragment a from begin. I would like to show the exact place on fragment a(say bottom) where the transaction begin.

I am doing all the fragment transaction in adapter of each item and code is like below.

Bundle arguments = new Bundle(); 
Fragment fragment = null; 
MyClass cls= (MyClass ) obj.get(position); 
arguments.putParcelable("single", offer); 
// Start a new fragment 
fragment = new DetailFragment(); 
fragment.setArguments(arguments); 
FragmentTransaction transaction = activity .getSupportFragmentManager().beginTransaction(); 
transaction.setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_out_right); 
transaction.replace(R.id.content_page_frame, fragment, OfferZoneDetailFragment.ARG_ITEM_ID); 
transaction.addToBackStack(DetailFragment.ARG_ITEM_ID); 
transaction.commit();

How to load the fragment transaction begin position when came back from other fragment.

Sree
  • 3,136
  • 2
  • 31
  • 39
  • It's depends on exact type of element that you want to show/keep position, have a look:http://stackoverflow.com/questions/15678489/best-way-save-restore-textview-position-in-scrollview & http://stackoverflow.com/questions/29208086/save-the-position-of-scrollview-when-the-orientation-changes & http://stackoverflow.com/questions/8619794/maintain-scroll-position-of-gridview-through-screen-rotation & http://stackoverflow.com/questions/3014089/maintain-save-restore-scroll-position-when-returning-to-a-listview – Movsar Bekaev Sep 22 '15 at 05:43
  • @MovsarBekaev how i can know exact type position ?, all the link says about the list and grid, i am asking about fragment with 5 grid and 1 list – Sree Sep 22 '15 at 05:45
  • I've repaired the links. You say that what you want is to save position of view area while you're switching the fragments, right? If so, you need to deal with those elements in your fragments, not with fragments themselves, as far as I know, you could do that by saving position in the Bundle or static var or anywhere else when switching and call some restore method on switching vice versa.. – Movsar Bekaev Sep 22 '15 at 05:48
  • ok that is a good idea i already think , one thing is the all the views are dynamically creating, i dot know how many view can be created. And all the fragment transactions are doing in the adapter – Sree Sep 22 '15 at 05:52

2 Answers2

2

I understand your problem. You can use

transaction.add(R.id.content_page_frame, fragment, OfferZoneDetailFragment.ARG_ITEM_ID); 

instead of

transaction.replace(R.id.content_page_frame, fragment, OfferZoneDetailFragment.ARG_ITEM_ID); 

May be, If it not solve your problem then use following mechanism, it will solve your problem:-

Check if your Fragment is already in your stack then attach already existing fragment.

else add new instanse of Frament.

use sacenario like this code :

android.support.v4.app.FragmentManager fm =   getSupportFragmentManager();
                AndroidFragment androidFragment = (AndroidFragment) fm.findFragmentByTag("android");
                AppleFragment appleFragment = (AppleFragment) fm.findFragmentByTag("apple");
                android.support.v4.app.FragmentTransaction ft = fm.beginTransaction();

                /** Detaches the androidfragment if exists */
                if(androidFragment!=null)
                    ft.detach(androidFragment);

                /** Detaches the applefragment if exists */
                if(appleFragment!=null)
                    ft.detach(appleFragment);

                /** If current tab is android */
                if(tabId.equalsIgnoreCase("android")){

                    if(androidFragment==null){
                        /** Create AndroidFragment and adding to fragmenttransaction */
                        ft.add(R.id.realtabcontent,new AndroidFragment(), "android");
                    }else{
                        /** Bring to the front, if already exists in the fragmenttransaction */
                        ft.attach(androidFragment);
                    }

                }else{    /** If current tab is apple */
                    if(appleFragment==null){
                        /** Create AppleFragment and adding to fragmenttransaction */
                        ft.add(R.id.realtabcontent,new AppleFragment(), "apple");
                     }else{
                        /** Bring to the front, if already exists in the fragmenttransaction */
                        ft.attach(appleFragment);
                    }
                }
                ft.commit();

EDIT :

transaction.add will show the previous fragment on backpress and will not go through the fragment life cycle

For Reference see this onTabChanged : http://wptrafficanalyzer.in/blog/creating-navigation-tabs-using-tabhost-and-fragments-in-android/

Sree
  • 3,136
  • 2
  • 31
  • 39
Hradesh Kumar
  • 1,765
  • 15
  • 20
  • @Sree, it's a great reply, I didn't think that attach/detach preserves the state, but in this case, how about transaction.show() and .hide() methods, can you check them please? I mean just showing one and hiding another, if this works it would be better for performance then attaching and detaching. – Movsar Bekaev Sep 22 '15 at 06:37
  • @MovsarBekaev I have checked that Attached and Detach preserves the State Of fragments.. – Hradesh Kumar Sep 22 '15 at 06:40
  • ok let me check, but i think when we press the back button show and hide is calling by fragment, and really add is giving more performance.In early on replace backbutton click lead to load the page once more, but in add just replace with a new fragment – Sree Sep 22 '15 at 06:42
  • @HradeshKumar, I'm not saying that it doesn't, I just wanted to know if the show() and hide() methods do the same. I was trying to emulate this problem but I had the problem with the IDE and I'm just gone curious)) – Movsar Bekaev Sep 22 '15 at 06:43
  • @Sree, I'm saying without removing and replacing the fragments, you attach them once and then just call hide() and show(), so you'll have 2 fragments always attached but only one will be shown at one moment. – Movsar Bekaev Sep 22 '15 at 06:46
  • I have to load some data from webservice in fragment call first time, so that will not work in my case.All are dynamic fragment, but if the data is loaded then we can use that – Sree Sep 22 '15 at 06:47
-1

Actually Both fragment in One Activity if you transfer from one fragment to another it has replace previous fragment that's why you can not retrieve previous fragment but it can be possible if you count back button position and set it to switch-case so it can be possible..

@Override
public void onBackPressed()
{
   super.onBackPressed();
   switch(pager.getselecteditem())
case 0:
exit();
break;
case:1
pager.setselecteditem(pager.getselecteditem()-1);
break;
}

Let me know if it helps you

Bpatel
  • 77
  • 8