In my application, from MainActivity I go to Fragment1 and from Fragment1 I replace Fragment1 with Fragment2. Now I want to remove Fragment1 from stack. How I do this?
When you are added the fragment,just add the TAG for that fragment.
With the use of that TAG you can easily remove your old fragment.
Eg.
FragmentManager fm = getSupportFragmentManager();
fm.replace(R.id.container,new MyFragment(),"TAG_FRAGMENT1").commit();
To remove old fragment use below code,
Fragment oldFragment = fm.findFragmentByTag("TAG_FRAGMENT1");
fm.beginTransaction().remove(oldFragment).commit();
After removing old fragment you can able to add new fragment.
Related
When I click an item from the SongList fragment, it should automatically switch to the NowPlaying fragment. Also how will I send the item position of the selected song from the recyclerView of the SongList fragment to the NowPlaying fragment? Thanks.
Access your NavController in your Fragment with getParentFragmentManager().findFragmentById(R.id.fragment_container_view) and call .navigate(R.id.now_playing).
I am creating tabbed fragments. The Main Activity is called media_main, as it is an activity I believe fragment transaction wont work- From this MainActivity there comes 3 tabbed fragments.
I need to know how to get to these through my Navigation Drawer? Do I FragmentTransaction to my Main Activity - is this even possible? or Am I stuck with just using a normal intent?
This is what I have:
case R.id.nav_media:
fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.main_container, media_main());
fragmentTransaction.commit();
getSupportActionBar().setTitle("Media");
item.setCheckable(true);
drawerLayout.closeDrawers();
break;
In my MainActivity I have a FrameLayout container for a Fragment.
One of the Fragments is a normal rectangular Button and the other one is a circular Button (both buttons have the same height).
When I click the Button I want the rectangular Button to transform into circular Button like this:
This is the code I currently have:
Fragment2 fragment = new Fragment2();
setSharedElementReturnTransition(TransitionInflater.from(getActivity()).inflateTransition(R.transition.change_image_trans));
setExitTransition(TransitionInflater.from(getActivity()).inflateTransition(android.R.transition.fade));
fragment.setSharedElementEnterTransition(TransitionInflater.from(getActivity()).inflateTransition(R.transition.change_image_trans));
fragment.setEnterTransition(TransitionInflater.from(getActivity()).inflateTransition(android.R.transition.fade));
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.button_container, fragment)
.addToBackStack("transaction")
.addSharedElement(view, "button_transition")
.commit();
The problem is when I click the button it transform into an elliptic shape and then to a circular button, but when I click the back button the animation is smooth.
So how to correct it to be like in the example above, or a sample close to that (I only need the button animation).
So my MainActivity has a navigation drawer with a set of fragments.
On top of the nav bar, I have 2 flags which represent a language. If an user clicks on a language, the app changes the language of the whole app.
Everything is working smoothly (activity refreshes, navigation drawer and all fragments get translated) but the fragment that's open doesn't. This means that the user needs to click on the navigation drawer and select the fragment again to see it translated.
How can I refresh the current fragment the user is in?
Add tag to your fragment when you commit it:
fragmentManager.beginTransaction().replace(R.id.your_id, fragment, "Your_Fragment_TAG").commitAllowingStateLoss();
Then You can detach and attach again your fragment for refresh:
// Reload current fragment
Fragment frg = null;
frg = getSupportFragmentManager().findFragmentByTag("Your_Fragment_TAG");
final FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.detach(frg);
ft.attach(frg);
ft.commit();
I'm currently working on creating a custom view which uses a xml layout file to render the view on the canvas. In this file I have a FrameLayout that is meant for dynamic content. I'm trying to change the content and in the custom view class with this method.
private void setFragment(Fragment fragment)
{
((ActionBarActivity) getContext()).getSupportFragmentManager()
.beginTransaction()
.replace(R.id.content_container, fragment)
.commit();
}
The problem with this is that I'm using the FragmentManager from the Activity this view is embedded in. Because of this the FragmentManager is referencing the Activity's layout file which does not contain my FrameLayout. I need this to be able to modify the content in the custom view's layout file so it can access the FrameLayout and change the content.
How can I go about accomplishing what I want? Any idea?
use callback pattern like here. But define an interface in your view class and implement it in activity.