I have Fragment XYZFragment where i display the list view.On the Listview item click i replace the Fragment like this.
Fragment fragment=new XYZFragment();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction ft= fragmentManager.beginTransaction();
ft.addToBackStack(null);
ft.replace(R.id.content_frame, fragment).commit();
But my problem is when i click back button the fragment reload the listview.It is never happen when i used to use Activity.
So my question is how to save the instance of previous fragment so that it will prevent the reloading of Data.
without seeing your code we can't help you out but from your question i can figure out the problem, this solution may help you out.
create stack such that
private static Stack<Fragment> myFragStack;
myFragStack = new Stack<Fragment>();
//To Load the fragment
public void loadFragment(Fragment fragment){
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
myFragStack.lastElement().onPause();
ft.hide(myFragStack.lastElement());
myFragStack.push(fragment);
}
//onBackPressed
public void onBackPressed() {
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
if (myFragStack.size() > 1) {
ft.remove(myFragStack.pop());
myFragStack.lastElement().onResume();
ft.show(myFragStack.lastElement());
ft.commit();
}
}
It's a sample code.. you can change it as per your requirement.
ft.replace() will completely remove the view & will lose the context so you can't maintain your list state, but using stacks maintaining fragments with hide-show will solve your problem.
Related
So, I have this problem: I am initializing a fragment - AddingTaskFragment
here's code:
Initializing AddingTaskFragment
private void initFragment()
{
// Get fragment manager
FragmentManager fm = getSupportFragmentManager();
// Begin transaction
FragmentTransaction ft = fm.beginTransaction();
// Create the Fragment and add
addingTaskFragment = new AddingTaskFragment();
ft.add(R.id.fragment_task_type_container, addingTaskFragment, "addTaskFragment");
// ft.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out);
// Commit the changes
ft.commit();
}
And it works fine
But then, I call some event, that replaces this fragment with other one(AddingScheduleFragment).
Replacing fragment
#Override
public void onScheduleTypePick()
{
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
// Create the fragment and attach book index
addingScheduleFragment = new AddingScheduleFragment();
// Replace the book list with the description
ft.replace(R.id.fragment_task_type_container, addingScheduleFragment, "addScheduleFragment");
ft.addToBackStack(null);
ft.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out);
ft.commit();
}
And when I'm poping my previous fragment(AddingTaskFragment) from the stack. All of my EditViews are gaining focus.
Returning previous fragment
#Override
public void onTaskTypePick()
{
getSupportFragmentManager().popBackStack();
}
What can be wrong? Why is this happening? Thanks for answers.
Important: When I replacing AddingTaskFragent with new object everything works great.
So, I've solved the problem by myself. Rather found the similar one.
afterTextChanged() callback being called without the text being actually changed
When the fragment was attached second time(because of .replace() method) it was restoring the previous state of the views. And then all textChangeListeners triggered.
I have implemented fragments in my application. Here my code for swiching fragment in fragment_container.
private void switchFragment(Fragment fragment, boolean isAddToBackStack, String tag)
{
android.support.v4.app.FragmentManager fm = getSupportFragmentManager();
android.support.v4.app.FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.container, fragment, tag);
if (isAddToBackStack)
ft.addToBackStack(tag);
setCurrentTopFragment(Integer.parseInt(tag));
ft.commit();
}
I have 4 fragments A,B,C and D and for switching between this fragmnets I am using above method. I have A,C,B in my backstack. If again I switch to fragmnet A, my backstack is like A,C,B,A. What I actually want is If I swich to A again I want backstack sequence like this C,B,A. Means Remove old instance from backstack and add new to it.
First get the back-stacked Fragment by id which you need to remove:
Fragment fragment = getSupportFragmentManager().getFragment(new Bundle(), TAG_KEY)
or there are several methods getBackStackEntryCount(), getBackStackEntryAt. After getting the fragment which you need to remove. Remove it from the fragment back-stack.
FragmentManager manager = getActivity().getSupportFragmentManager();
FragmentTransaction trans = manager.beginTransaction();
trans.remove(fragment);
Then you can add a new fragment Done :)
I have a fragment in which there is a nested fragment which I add in this way:
if (home == null) {
home = new MyFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
transaction.addToBackStack(MyFragment.class.getName());
transaction.add(R.id.child_fragment, home).commit();
}
When I enter another fragment and go back the child fragment from above is not there. I checked and the instance is different from null.
UPDATE: I changed the code as suggested by Ashwin S Ashok but it's still not working.
Try using these methods:
// Use this if you don't want to retain the fragment.
protected void replaceFragmentStack(int container, Fragment fragment) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(container, fragment);
fragmentTransaction.commit();
}
// Use this if you want to add the fragments in a stack.
protected void addFragmentStack(int container, Fragment fragment, String tag) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
fragmentTransaction.add(container, fragment, tag);
fragmentTransaction.addToBackStack(tag);
fragmentTransaction.commit();
}
I would suggest you to use getChildFragmentManager() when making transactions inside a fragment. And its a bug i guess.
You can check out this thread it will help you alot Android 4.2: back stack behaviour with nested fragments
Also you need to go through The Curious Techizen's blog
Here is the link for the github project sample for same mechanism
I hope this will help you.
I have used multiple Fragments in my Project. I want to save a Fragment's state and restore this state when I come back to this. In this Fragment I show multiple images which change on button click. I use the following code for this:
String backStateName = fragment.getClass().getName();
FragmentManager fragmentManager = getSupportFragmentManager();
boolean fragmentPopped = fragmentManager.popBackStackImmediate(backStateName, 0);
if (!fragmentPopped) {
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container_body, fragment);
fragmentTransaction.addToBackStack(backStateName);
fragmentTransaction.commit();
}
It works fine & it saves state, but it does not show previous image's.
Any help, suggestion or tutorials would be highly appreciated. Thank you.
I am trying with the fragment list ....
Initilize the framents list
static List<String> fragments = new ArrayList<String>();
Code to fragment change and take in back stack
String backStateName = fragment.getClass().getName();
FragmentManager manager = getSupportFragmentManager();
//fragment not in back stack, create it.
FragmentTransaction ft = manager.beginTransaction();
if(!fragments.contains(backStateName)) {
// ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
// ft.setCustomAnimations(R.anim.fade_in, R.anim.fade_out);
ft.replace(R.id.frame, fragment);
ft.addToBackStack(backStateName);
ft.commit();
fragments.add(backStateName);
System.out.println("backStateName" + fragments);
}
else
{
ft.replace(R.id.frame, fragment);
ft.commit();
}
onBackpressed
#Override
public void onBackPressed() {
if(fragments.size()>0)
{
fragments.remove(fragments.size()-1);
}
super.onBackPressed();
}
for back remove stack
final android.app.FragmentManager fm = getFragmentManager();
fm.addOnBackStackChangedListener(new android.app.FragmentManager.OnBackStackChangedListener() {
#Override
public void onBackStackChanged() {
if (getSupportFragmentManager().getBackStackEntryCount() ==0) {
// dLayout.closeDrawers();
finish();
}
else
{
// dLayout.closeDrawers();
}
}
});
You can use the following code.
It will not reload the fragment when you come back from previous fragment:
if (!fragmentPopped) {
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.hide(getFragmentManager().findFragmentById(R.id.container_body));
fragmentTransaction.add(R.id.container_body, fragment);
fragmentTransaction.addToBackStack(backStateName);
fragmentTransaction.commit();
}
You can use
fragmentTransaction.hide(getFragmentManager().findFragmentById(R.id.frame_container));
fragmentTransaction.add(R.id.frame_container, fragment);
fragmentTransaction.addToBackStack(null);
Instead of
fragmentTransaction.replace(R.id.container_body, fragment);
fragmentTransaction.addToBackStack(backStateName);
If you can save the images in the android disk cache when you open the fragment and display the images by reading from the disk cache it might resolve your problem. Follow the below link from google on how to save images in disk cache.
https://developer.android.com/training/displaying-bitmaps/cache-bitmap.html
Below are the steps i think you should follow.
Store your images in the disk cache.
onAttaching your ImagesFragment to the Activity read the images from the disk cache and show them
when the fragment is popped from the backstack your code will automatically read the images from the disk cache.
Hope this helps.
I used multiple fragments with this code.Put this code in your MainActivity where's your framelayout or fragment.
#Override
public void onBackPressed() {
/* Fragment fragment = new SettingActivity();
String title = getString(R.string.app_name);
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container_body, fragment);
fragmentTransaction.commit();
// set the toolbar title
getSupportActionBar().setTitle(title);*/
//Log.e("title:", getTitle().toString());
getSupportActionBar().setTitle("Home");
//Log.e("title:", getTitle().toString());
if (!getTitle().toString().equals("Home")) {
Fragment fragment = new Activity_category();
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container_body, fragment).commit();
this.setTitle("Home");
} else {
super.onBackPressed();
}
}
May this will help you.
You taking your fragment in back stack so its didn't fault of your fragment you need to check in your image library. May i know which library are you using. I recommended to use picasso library. I am also using this and getting state saved image in my fragment.
http://square.github.io/picasso/
You can do circular navigation by using below code. You can visit even two fragments as vice-versa by endless.
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
hideLastFragment(fragmentTransaction);
fragmentTransaction.add(R.id.profile_fragmentContainer, youfFragment, fragmentTagName).addToBackStack(fragmentTagName);
fragmentTransaction.commitAllowingStateLoss();
/**
* To hide the last fragment
*
* #param fragmentTransaction FragmentTransaction
*/
public void hideLastFragment(FragmentTransaction fragmentTransaction) {
final int backStackCount = getSupportFragmentManager().getBackStackEntryCount() - 1;
if (backStackCount >= 0) {
String fragmentName = getSupportFragmentManager().getBackStackEntryAt(backStackCount).getName();
Fragment lastFragment = getSupportFragmentManager().findFragmentByTag(fragmentName);
fragmentTransaction.hide(lastFragment);
}
}
I am developing a simple android app only for tablets and using android 4.0. My application have the main screen like as follow:
Oncreate() of Main Activity I am adding Fragment A in the main.xml using following code:
FragmentTransaction ft = getFragmentManager().beginTransaction();
Fragment imageFragment = new ImageFragment();
ft.replace(R.id.fragment_container, imageFragment);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.addToBackStack(null);
ft.commit();
This fragment A just have only a Image view which is clickable. Now I want that when user click on Image view then another fragment (Fragment B) should call and it replace the image view. The Fragment B have a VideoView which play the video.
So My second screen should be like as follow:
My problem is I am not gettting "How to call second fragment from the first one with in main screen activity?"
I can use different activities but I do not want to do so and just want to run this using fragments.
Please guide me.
This is the simplest way:
1) Inside YourActivitycreate a method:
public void goToSecondFragment(){}
FragmentTransaction ft = getFragmentManager().beginTransaction();
Fragment secondFragment = new SecondFragment();
ft.replace(R.id.fragment_container, secondFragment);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.addToBackStack(null);
ft.commit();
}
2) In your first fragment, when you want to replace it, call:
YourActivity activity = (YourActivity) getActivity();
activity.goToSecondFragment();
You can do the same using below:
Adding a fragment with maintaining a back stack
FragmentManager supportFragmentManager = getSupportFragmentManager();
supportFragmentManager.addOnBackStackChangedListener(new OnBackStackChangedListener() {
#Override
public void onBackStackChanged() {
}
});
FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
fragmentTransaction.addToBackStack("fragmentTag");
fragmentTransaction.replace(R.id.slate, fragment, "fragmentTag");
fragmentTransaction.commit();
And Replacing a fragment
FragmentManager supportFragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
fragmentTransaction.replace(R.id.slate, fragment, "fragmentTag");
fragmentTransaction.commit();
Should work for you.