saving fragment transaction whereever - android

initially I have an activity A in which I'm gonna open one fragment so here how can i save that fragment
So, that when I launch my application after destroying it restores that fragment in that same Activity in same position
For, answering convenience here's my fragment transaction code:
Fragment newFragment = new ece_frag();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.setCustomAnimations(R.anim.fade_in,R.anim.fade_out);
transaction.replace(R.id.frame_layout, newFragment);
transaction.commit();

Lets say you have 3 fragments A,B and C.
I give index to each fragment like this say 0->A, 1->B, 2->C. So when i do this, I also save the index like the code below:
Fragment newFragment = new A();
FragmentTransaction transaction =
getFragmentManager().beginTransaction();
transaction.setCustomAnimations(R.anim.fade_in,R.anim.fade_out);
transaction.replace(R.id.frame_layout, newFragment);
transaction.commit();
SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt("last_fragment", 0);//For fragment A saving index 0
editor.commit();
And then in onCreate you can use an if case like this:
SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
//0 here is the default value
int lastFragment = sharedPref.getInt("last_fragment", 0);
Then you can do this
switch(lastFragment){
case 0:
//Load your fragment Here according to the index.
break;
case 1:
//Load your fragment Here according to the index.
break;
case 2:
//Load your fragment Here according to the index.
break;
}
Hope this helps.

Related

invoke fragment from a fragment

by pressing a button inside the main fragment, I wanna call the second fragment in that place
public void onViewClicked(View view) {
Fragment frag = null;
switch (view.getId()) {
case R.id.btn_login:
frag = new LoginFragment();
break;
case R.id.btn_offline:
frag = new OfflineFragment();
break;
}
MainActivity mainActivity = new MainActivity();
FragmentManager manager =
mainActivity.getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.fragment, frag);
transaction.commit();
}
Never create an Activity instance . Its a Component and managed by system itself(Intent). Use getActivity() to get the Context of Activity.
FragmentManager manager =
getActivity().getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.fragment, frag);
transaction.commit();
You should take a look at Android Application Fundamentals.
I would suggest to go with getChildFragmentManager(); of the fragment to get the fragment manager to perform all your fragment related operations instead of getting it from using the fragmentManager from the getActivity() method. It help you a lot on reloading the fragment on resume of it again at later point of time.

Fragment not showing previous data when coming from another fragment

In my android activity I am using multiple fragments, I am successfully switching these fragments according to my requirement but problem is that when I am switching Fragment 1 to Fragment 2 and back from Fragment 2 to Fragment 1, Fragment 1 not showing previous data Fragment 1 start from stretch, but I want to show previous data same as I was selected.
Here is my code for go Fragment 1 (fragment_search_customerProfile) to Fragment 2 (fragment_CustomerInfo):
FragmentManager manager = getActivity().getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.filterFram, new fragment_Search());
FrameLayout layout = (FrameLayout) rootView.findViewById(R.id.cpFrame);
layout.removeAllViewsInLayout();
transaction.remove(new fragment_search_customerProfile()).commit();
Here is my code for back Fragment 1 (fragment_search_customerProfile) fromFragment 2 (fragment_CustomerInfo):
FragmentManager manager = getActivity().getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
Log.d("fragment_Search", fromSearch + "");
transaction.replace(R.id.custIndoFram, new fragment_search_customerProfile());
FrameLayout layout = (FrameLayout) rootView.findViewById(R.id.custIndoFram);
layout.removeAllViewsInLayout();
transaction.remove(new fragment_CustomerInfo()).commit();
Can anyone explain me how can I stay save my fragment data?
Instead of transaction.replace(R.id.filterFram, new fragment_Search());
you can use transaction.add to add fragment while having the data of first one
transaction.add(R.id.filterFram, new fragment_Search());
transaction.addToBackStack(null);
From fragment two you can use .show instead of replace to show the first fragment
and hide fragment two from the first one. transaction.show(getSupportFragmentManager().findFragmentByTag("firstFragmentTag")).commit();
You can simple keep the instances of the fragments, so in your Activity you would have fields like that.
private Fragment fragment1;
private Fragment fragment2;
And now you can use the sexy replace() option,
To add Fragment1
if (fragment1 == null) {
fragment1 = new fragment_Search();
}
FragmentManager manager = getActivity().getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.filterFram, fragment1);
and same for Fragment2
And you can hold the state of the fragment (data it retrieves) in the Fragment itself, and in your onViewCreated() you check, if the state is not null, you update the view immediately.
class Fragment1 extends Fragment {
private State state;
public void onViewCreated(View v) {
if (state != null) {
// Update UI here
}
}
}
UPDATE
Using your own code
private Fragment fragment1; // This is a field outside of below method
if (fragment1 == null) {
fragment1 = new fragment_Search();
}
FragmentManager manager = getChildFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.filterFram, fragment1).commit();
FrameLayout layout = (FrameLayout) rootView.findViewById(R.id.cpFrame);
layout.removeAllViewsInLayout();
Going to fragment2 should be the same, and this code is used to go to Fragment1 and Fragment2, doesn't matter if you are going back or first time.
You have to add fragment instead of remove or replace

not able to save 2nd fragment state as it goes to new activity

i have 2 fragments (1,2).
after a button click on (fragment 1) transition happens to (fragment 2) .now from (fragment 2) if a button is clicked i go to the (new_activity ).my problem is when i return from (new_activity) i want to go back to (fragment 2) & then to (fragment 1) but it directly goes to fragment 1 .i am not able to add (fragment 2) to the addTOBackStack.
// method to handle click event of category
public void onClick(View v) {
FragmentManager fragmentManager = getActivity()
.getSupportFragmentManager();
FragmentTransaction ft = fragmentManager.beginTransaction();
Bundle bundle = new Bundle();
int categoryId;
CategoryFragment a = new CategoryFragment();
Log.d("HomeFragment", v.getId() + "");
switch (v.getId()) {
case R.id.image_american_chiffon:
categoryId = 1;
bundle.putInt("categoryId", categoryId);
a.setArguments(bundle);
ft.replace(R.id.contentFrame, a);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.addToBackStack(null);
break;
case R.id.image_nagma:
categoryId = 2;
bundle.putInt("categoryId", categoryId);
a.setArguments(bundle);
ft.replace(R.id.contentFrame, a);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.addToBackStack(null);
break;
}
ft.commit();
Log.e("BACK STACK COUNT",fragmentManager.getBackStackEntryCount()+"");
}
Everytime you call replace method,new object of that fragment is being created.You can add all needed fragments at the beginning in the hidden state and then just show and hide according to your need.so you don't need to initialize everytime,just have to display the required fragment and hide the other fragment.
To Add fragment at the begining,do like this:
Fragment myFragment = new CategoryFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.add(R.id.fragmentContainer, myFragment, SOME_TAG);
transaction.hide(myFragment);
transaction.commit();
and Instead of calling replace() method,just hide and show the desired fragment
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.show(getFragmentManager().findFragmentByTag(fragmentTagToBeShown));
transaction.hide(getFragmentManager().findFragmentByTag(currentFragmentTag));
transaction.commit();

how to switch from one fragment to another fragment android

Hi I have 5 fragments Now first time when I go to Activity of that fragments then default First fragment is called,then on first Fragment there is a button by clicking that button I go to the second fragment,similarly in Second fragment There is a button and clicking that button I go to the third fragment and so on.
Now My Question is that Current I am on Fifth fragment ,Now I want to go fifth fragment to second fragment,what should I do for this?
Can any one please tell me?
You can pop the fragment by name. While adding fragments to the back stack, just give them a name.
fragmentTransaction.addToBackStack("frag2");
Then in fragment5, pop the back stack using the name ie.. frag2 and include POP_BACK_STACK_INCLUSIVE
someButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
FragmentManager fm = getActivity()
.getSupportFragmentManager();
fm.popBackStack ("frag2", FragmentManager.POP_BACK_STACK_INCLUSIVE);
}
});
Here is the method to add and replace fragment.
public void addReplaceFragment(Fragment fragment, int addOrReplace) {
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
switch (addOrReplace) {
case addFragment:
transaction.add(R.id.frame_containerforsearchable, fragment);
transaction.commit();
break;
case replaceFragment:
transaction.replace(R.id.frame_containerforsearchable, fragment);
transaction.commit();
break;
default:
break;
}
}
call of fragment is..
addReplaceFragment(currencyFragmentWithSearch, replaceFragment);
replaceFragment integer variable
currencyFragmentWithSearch Fragment instance
You can always do this :)
getActivity().getSupportFragmentManager().replace(R.id.yourFrameLayout,new Fragment2(),"FRAG2");
OR
Fragment fragment = getActivity().getSupportFragmentManager().findFragmentByTag("FRAG2");
getActivity().getSupportFragmentManager().replace(R.id.yourFrameLayout,,"FRAG2");
Try this code:
Fragment fragment = new SecondFragmentName();
FragmentManager fm = getActivity().getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.content_frame, fragment);
ft.commit();
just small correction to mudit_sen code. It is working, only if you add .beginTransaction. Thank you.
getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.FrameLayoutId,new FragmentName()).commit();

Proper way of saving Fragment transaction before exiting

I have my method for replacing Fragments in my mainActivity..like this after clicking my Button...
FragmentTransaction trans = getSupportFragmentManager().beginTransaction();
Fragment2 f2=new Fragment2();
trans.replace(R.id.root_frame, f2,"Fragment2");
trans.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
trans.commit();
getPager().setCurrentItem(0);
and i need to save this change before exiting my app..so when i re-open my app transaction changes would be there...
You could save your fragment state in shared preferences and retrieve it when the fragment gets recreated.
Save your state when you set the transaction
SharedPreferences mypreferences=getActivity().getPreferences(MODE_PRIVATE);
Editor editor= mypreferences.edit();
editor.putString("transition","FragmentTransaction.TRANSIT_FRAGMENT_OPEN");
//do the same for other fragment states
editor.commit();
Retreive your state in your onCreate() method.
String transition= mypreferences.getString("transition", "");
if(!transition.equals(""){
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
//do the same for other fragment states
}

Categories

Resources