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;
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).
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.
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();
My school assignment has an activity that contains a ListFragment and a Fragment. The MainActivity creates the ListFragment, and then each item on the list should open the regular Fragment that just contains a picture, and then when I click on the picture it should go back to the ListFragment.
In all cases, I call the fragments using this code (edited for the specific case of course):
Fragment fragTwo = new FragmentTwo();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.fragment_place, fragTwo);
ft.addToBackStack(null);
ft.commit();
It calls the fragments OK, except that the second fragment will appear OVER the ListFragment, and I can still select things from the list while the second fragment is open. Do I need to create a new fragment object every time I navigate? And how do I disable/enable a fragment from it's own onclick listener?
main_activity.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<fragment
android:name="com.example.w0068332.fragmentstest.FragmentOne"
android:id="#+id/fragment_place"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
1- Yes you need to create a fragment for each item.
Bundle arguments = new Bundle();
arguments.putInt(FragmentTwo.ARG_ITEM_ID,R.drawable_pic);
FragmentTwo fragment = new FragmentTwo();
fragment.setArguments(arguments);
getSupportFragmentManager().beginTransaction()
.replace(R.id.item_detail_container, fragment)
.commit();
I suppose that you display pictures from resources
2- To disable selection from ListFragment while displaying the second one.
In the parent layout of the second fragment you have to add background and make it clickable as below:
android:background="#color/white"
android:clickable="true"
3- To navigate back for the ListFragment add a click listener for the ImageView and onClick call onBackPressed of the Activity:
getActivity().onBackPressed();
you can also have a look on a template from Android studio for Master and details activity