Proper way of saving Fragment transaction before exiting - android

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
}

Related

how to save api data in fragment back stack from fragment two to fragment one on back button clicked

i have two fragments,in first fragment i have list of invoice details with check box in list view .
i need to store the list items while back from the second fragment
1st fragment:
MakePayment debtorsAging = MakePayment.newInstance();
debtorsAging.setArguments(bundle);
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.content_frame, debtorsAging,MakePayment.TAG);
fragmentTransaction.addToBackStack(PaymentCollection.class.getName());
fragmentTransaction.commit();
2nd fragment:
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction ft=fragmentManager.beginTransaction();
ft.remove(new MakePayment());
ft.commit();
fragmentManager.popBackStackImmediate();
need to restore list of invoices with checked items on go back from makePayment fragment to paymentCollection fragment
If you back to previous activity by onBackPressed (not by transaction) i think you cant pass anythig between fragments by bundle or something. One of resolution I suggest to save id's or something to SharedPreferences before back to previous activity and restore them in first fragment

saving fragment transaction whereever

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.

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();
}

Detecting when a fragment is on top again (has focus)

In my app, my activity has a container with a fragment. When a button in that fragment is clicked, I add a new fragment to the container and add the previous fragment to the backstack.
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.primary_fragment_container, homeFragmentHandler);
fragmentTransaction.addToBackStack(folderId.toString());
fragmentTransaction.commit();
The result is that pressing back closes the top fragment and returns me to the previous fragment, which is exactly how I want it. HOWEVER there are certain elements on the fragment that should be refreshed at this point. When the user clicks back and is returned to the previous fragment, how do I know to refresh the data within that fragment? is there a method such as onResume() for this scenario?
You have to proper add fragments to backstack:
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
MyFragment fragment = new MyFragment ();
transaction.replace(R.id.primary_fragment_container, fragment).addToBackStack(null).commit();
Fragment homeFragmentHandler= new Fragment();
Bundle bundle = new Bundle();
//replace this line below wth something convinient
bundle.putInt("key", value);
fragment.setArguments(bundle);
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.primary_fragment_container, homeFragmentHandler);
fragmentTransaction.addToBackStack(folderId.toString());
fragmentTransaction.commit();
This will send data regarding which fragment/ activity caused the previous fragment to load. Now in its onCreate add this code to check the data in the bundle
//In the onCreate of the original fragment
Bundle bundle = this.getArguments();
if(bundle.getInt(key, defaultValue)!=null{
int myInt = bundle.getInt(key, defaultValue);
// Load the data that needs to be refreshed when original fragment is loaded from the second one
}
No else statement is needed. If the fragment is made from any other activity/ fragment then the bundle.getInt will return null and hence that code inside the statement wont be executed.

Retain same instance of Fragment on configuration Change

I have the following issue. My application has 2 fragments, the first one that is executed show a list of items, when you tap on an item, shows a detail fragment.
I also have a NavigationDrawerActivity, with the following methods to handle the stack of fragments.
private void changeFragment(int pos, Fragment fragment, boolean addToBackStack) {
Fragment f = getSupportFragmentManager().findFragmentByTag(String.valueOf(pos));
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction ft = fragmentManager.beginTransaction();
mCacheServices.setmFragmentTag(String.valueOf(pos));
ft.replace(R.id.container, fragment, String.valueOf(pos));
if ((addToBackStack)) {
ft.addToBackStack(STACK_KEY);
}
ft.commit();
}
The problem i am facing is when i am in the DetailFragment, and i rotete the device, it goes to the ItemList fragment, which is the firs fragment excuted by the app. What can i do to mantain on the DetailFragment when i rotate the device?
Thanks!
you can save the state of the fragment and then re-create it. Go through this for more details
http://developer.android.com/guide/topics/resources/runtime-changes.html
hope this helps.

Categories

Resources