Show hidden Fragment - java

I understand there were a lot of such issues. But a strange situation is happening with my option. Well, it’s not very clear. You can tell what is wrong.
I hide activeFragment if it is not null. Then I add a Fragment if it is not. And if there is, then I show. From the first time, everything works fine when I create it using the add() method or replace(). But if I go back and try to show() the already hidden Fragment, then it's just a white screen, there is no Fragment. What could be the problem?
private void loadFragment(Fragment fragment, String tag) {
if (fragment != null) {
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
if (activeFragment != null) {
transaction.hide(activeFragment);
}
if (fm.findFragmentByTag(tag) == null) {
transaction.add(R.id.fragment_container, fragment, tag);
} else {
transaction.show(fragment);
}
activeFragment = fragment;
transaction.commit();
}
}

Related

how to use fragments backstack with shared element transition

shared element transition replace the fragment, so I can't add it to the backstack and call popbackstack when the back arrow button is pressed.
I have a main activity with inside a mainfragment, the main fragment handle a table layout so each tab is a fragment with inside a recycler view, when a recycler view element is clicked the shared element transition start to a new fragment that shows element details.
This is the adapter, where the element is clicked:
holder.image.setTransitionName("transition" + holder.getAdapterPosition());
if (fragment instanceof tab1_anime) {
((tab1_anime) fragment).openShowElementFragment(holder.getAdapterPosition(), v.findViewById(R.id.main_image));
}
This is the openShowElementFragment inside my tab fragment:
public void openShowElementFragment(int position, View view) {
AddElement element = anime_list.get(position);
ShowElementFragment showElementFragment = new ShowElementFragment();
Bundle bundle = new Bundle();
bundle.putString("transitionName", "transition" + position);
bundle.putSerializable("element", element);
bundle.putInt("position", position);
bundle.putInt("from", 0);
showElementFragment.setArguments(bundle);
((MainActivity) context).showFragmentWithTransition(this, showElementFragment, "showElementFragment", view, "transition" + position);
}
this is the openshowelementfragment function called in the previous code block:
public void showFragmentWithTransition(Fragment current, Fragment newFragment, String tag, View sharedView, String sharedElementName) {
FragmentManager fragmentManager = getSupportFragmentManager();
// check if the fragment is in back stack
boolean fragmentPopped = fragmentManager.popBackStackImmediate(tag, 0);
if (fragmentPopped) {
} else {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
current.setSharedElementReturnTransition(TransitionInflater.from(this).inflateTransition(R.transition.default_transition));
current.setExitTransition(TransitionInflater.from(this).inflateTransition(R.transition.default_transition));
newFragment.setSharedElementEnterTransition(TransitionInflater.from(this).inflateTransition(R.transition.default_transition));
newFragment.setEnterTransition(TransitionInflater.from(this).inflateTransition(R.transition.default_transition));
}
fragmentManager.beginTransaction()
.replace(R.id.fragmentHolder, newFragment)
.addToBackStack(null)
.addSharedElement(sharedView, sharedElementName)
.commit();
}
}
and this is the backarrow button:
back_arrow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
getSupportFragmentManager().popBackStack();
}
});
If I try to add the new fragment instead of replace the old one, then there is not animations at all.
If I try to replace the old fragment with the new one and using anyway the addtobackstack(null) then the shared element transition works from start to end BUT the fragment at the end is without data, empty:
I tried also:
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragmentHolder, new MainFragment())
.addToBackStack(null)
.commit();
but in this way the shared element transition doesn't work on exit:
I think your problem is related to exit transition as you get solution of empty list, now for exit transition effect check below code.
public void showFragmentWithTransition(Fragment current, Fragment newFragment, String tag, View sharedView, String sharedElementName) {
FragmentManager fragmentManager = getSupportFragmentManager();
// check if the fragment is in back stack
boolean fragmentPopped = fragmentManager.popBackStackImmediate(tag, 0);
if (fragmentPopped) {
} else {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
current.setSharedElementReturnTransition(TransitionInflater.from(this).inflateTransition(R.transition.default_transition));
current.setExitTransition(TransitionInflater.from(this).inflateTransition(R.transition.default_transition));
newFragment.setSharedElementEnterTransition(TransitionInflater.from(this).inflateTransition(R.transition.default_transition));
newFragment.setEnterTransition(TransitionInflater.from(this).inflateTransition(R.transition.default_transition));
newFragment.setExitTransition(TransitionInflater.from(this).inflateTransition(R.transition.default_transition));
newFragment.setSharedElementReturnTransition(TransitionInflater.from(this).inflateTransition(R.transition.default_transition));
}
getSupportFragmentManager().beginTransaction()
.addSharedElement(sharedElement, transitionName)
.replace(R.id.container, newFragment)
.addToBackStack(null)
.commit();
}
}
Here I add two new lines for exit transitions.
newFragment.setExitTransition(TransitionInflater.from(this).inflateTransition(R.transition.default_transition));
newFragment.setSharedElementReturnTransition(TransitionInflater.from(this).inflateTransition(R.transition.default_transition));
If its not work then follow below link, I think it will help you.
https://medium.com/#bherbst/fragment-transitions-with-shared-elements-7c7d71d31cbb
Your so called sharedElementName is actually a transitionName.
a) The reference example doesn't directly manipulate the backstack:
Android Fragment Transitions: RecyclerView to ViewPager & Blog.
b) Manual transactions might require another order of method calls:
getSupportFragmentManager().beginTransaction()
.addSharedElement(sharedElement, transitionName)
.replace(R.id.container, newFragment)
.addToBackStack(null)
.commit();
Also see this example, how alternate transitions could be be set, compared to:
TransitionInflater.from(this).inflateTransition(R.transition.default_transition)
Whatever R.transition.default_transition may be.
It may look strange when using the same transition for both ways, instead of enter/exit transitions, alike NavAction would feature. FragmentNavigator.Extras could be used to apply enter/exit transitions, when using navigation component; this also can be combined with ActionBar. Option a) is probably less complicated. b) something alike this Kotlin example might make more sense.
It's pointless to build clunky navigation FX when enter/exit transitions would be supported out-of-the-box. Maybe consider loading with Glide, when loading images.
Assume the following build.gradle; there's no need to reinvent the wheel.
dependencies {
androidTestImplementation "androidx.navigation:navigation-testing:2.5.3"
implementation 'androidx.navigation:navigation-runtime:2.5.3'
implementation 'androidx.navigation:navigation-fragment:2.5.3'
implementation 'androidx.navigation:navigation-ui:2.5.3'
annotationProcessor 'com.github.bumptech.glide:compiler:4.14.2'
implementation 'com.github.bumptech.glide:glide:4.14.2'
}
Then Navigation with FragmentNavigator.Extras might rather be the current method:
Bundle navArgs = new Bundle();
navArgs.putInt("position", position);
FragmentNavigator.Extras extras = new FragmentNavigator.Extras.Builder()
.addSharedElement(sharedElement, transitionName)
.build();
Navigation.findNavController(view).navigate(
R.id.details, // ID of Nav destination
navArgs, // Bundle of args
null, // NavOptions
extras);
One can also define transitionName in XML <action/> nodes.

Can not add Fragment

I've been stuck on this for a while now and will appreciate any guidance on this. Also not sure if my title is accurate. Inside my adapter I create a new fragment:
Adapter code:
cardAdoptDetailsFrag nextFrag = new cardAdoptDetailsFrag();
android.support.v4.app.Fragment callingFrag = ((FragmentActivity)context).getSupportFragmentManager().findFragmentByTag("TagFeedFragment");
FragmentTransaction ft = ((FragmentActivity)context).getSupportFragmentManager().beginTransaction();
ft.hide(callingFrag);
ft.add(R.id.fram, nextFrag,"cardAdoptDetailsFrag");
ft.addToBackStack("TagFeedFragment");
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();
Then inside my Main Activity where I manage all the fragments, I'm trying to check if the "cardAdoptDetailsFrag" isAdded. For some reason I cannot do it as per the below.
Get Fragment by tag. The below is where it fails with null object reference.
Fragment frag = getSupportFragmentManager().findFragmentByTag("cardAdoptDetailsFrag");
Log.d(TAG, "Check if added: "+frag.isAdded());
Now I know I can just add it inside a method and do a null check and return a boolean, but I know I'm doing something wrong here. Because with my other fragments the isAdded and remove works, but they get initiated inside the Main Activity where "cardAdoptDetailsFrag" gets initiated inside the adapter.
Example of Main Activity:
public class MainActivity extends AppCompatActivity implements adoptFeedFragment.OnFragmentInteractionListener,
lostAndFoundFragment.OnFragmentInteractionListener,
servicesFragment.OnFragmentInteractionListener,
userMenuFragment.OnFragmentInteractionListener,
showUserAdoptPostsFrag.OnFragmentInteractionListener{
adoptFeedFragment adoptFeedFragment;
lostAndFoundFragment lostAndFoundFragment;
servicesFragment servicesFragment;
userMenuFragment userMenuFragment;
....
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adoptFeedFragment = new adoptFeedFragment();
lostAndFoundFragment = new lostAndFoundFragment();
servicesFragment = new servicesFragment();
userMenuFragment = new userMenuFragment();
....
//Here I can do for example:
adoptFeedFragment.isAdded(); //Will simply return a boolean.
//Or I can do a remove:
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.remove(adoptFeedFragment);
//But I cannot do the below:
Fragment frag = getSupportFragmentManager().findFragmentByTag("cardAdoptDetailsFrag");
Log.d(TAG, "frag.isAdded(): "+frag.isAdded());
*****Edited Post. Ok lets say the frag is added. Why can I not remove it using the below.
public boolean isAdoptDetailsFragAdded() {
Fragment frag = getSupportFragmentManager().findFragmentByTag("cardAdoptDetailsFrag");
if(frag == null){
return false;
} else {
return true;
}
}
public Fragment getAdoptDetailsFrag() {
return getSupportFragmentManager().findFragmentByTag("cardAdoptDetailsFrag");
}
//I'm unable to remove the fragment using the below:
Log.d(TAG, "showFeedFragment: "+isAdoptDetailsFragAdded()); <--returns true
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
if(isAdoptDetailsFragAdded()) {
ft.remove(getAdoptDetailsFrag());
}
ft.commit();
//Now I check again wether it is still added. And still returns true even though I just removed it.
Log.d(TAG, "showFeedFragment: "+isAdoptDetailsFragAdded()); <-- Still returns true.
}
This is my fragment adding or replacing method which i am using in 13 projects.
You can use it and get rid of managing fragments.
/**
* replace or add fragment to the container
*
* #param fragment pass android.support.v4.app.Fragment
* #param bundle pass your extra bundle if any
* #param popBackStack if true it will clear back stack
* #param findInStack if true it will load old fragment if found
*/
public void replaceFragment(Fragment fragment, #Nullable Bundle bundle, boolean popBackStack, boolean findInStack) {
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
String tag = fragment.getClass().getName();
Fragment parentFragment;
if (findInStack && fm.findFragmentByTag(tag) != null) {
parentFragment = fm.findFragmentByTag(tag);
} else {
parentFragment = fragment;
}
// if user passes the #bundle in not null, then can be added to the fragment
if (bundle != null)
parentFragment.setArguments(bundle);
else parentFragment.setArguments(null);
// this is for the very first fragment not to be added into the back stack.
if (popBackStack) {
fm.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
} else {
ft.addToBackStack(parentFragment.getClass().getName() + "");
}
ft.replace(R.id.main_frame_container, parentFragment, tag);
ft.commit();
fm.executePendingTransactions();
}
Here R.id.main_frame_container is FrameLayout in activity layout on which fragment is placed.
Here findFragmentByTag() is returning null when you do getSupportFragmentManager().findFragmentByTag("cardAdoptDetailsFrag").
findFragmentByTag() will return null in two cases.
If fragment cardAdoptDetailsFrag is not yet attached to view (you did not call add or replace for this fragment)
You added that fragment, but you are using different TAG, while searching that fragment.
Make sure if non of the case in your problem.
Now the solution is, you can modify your logic, like
Fragment frag = getSupportFragmentManager().findFragmentByTag("cardAdoptDetailsFrag");
if (frag != null && frag.isAdded()) {
// Do you task. Fragment is added
} else {
// Fragment is not added yet
}
I did not went through the long post but I read only this part.
Now I check again wether it is still added. And still returns true even though I just removed it.
And the reason behind that is FragmentTransaction.commit is not synchronous.
Read the docs for commit method:
*Schedules a commit of this transaction. The commit does
* not happen immediately; it will be scheduled as work on the main thread
* to be done the next time that thread is ready.*
If you need that to be synchronous, use commitNow instead.

How to go to root activity from any fragment by pressing back button?

i have an activity with a navigation drawer built using the library https://github.com/mikepenz/MaterialDrawer
I want to load 4 different pages from selecting 4 different items from the navigation drawer.
I decided to use fragments for each item in the navigation drawer, so when any item is pressed, a new fragment for that option is created and shown to the user.
What i want is that, regardless of which fragment the user is currently in, or which fragment he came from, whenever he presses the back button, he is returned to the original activity.
This is the code i have so far, but apparently the Fragment Backstack is not being updated.
result = new DrawerBuilder().withSelectedItem(-1)
.withActivity(this)
.withToolbar(toolbar)
.withAccountHeader(headerResult)
.addDrawerItems(
item1,item2,item3,item4
)
.withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
#Override
public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
result.setSelection(position,false);
clearStack();
switch (position) {
case 1:
fragment = new admin_fragment_data_entry();
break;
case 2:
fragment = new admin_fragment_data_edit();
break;
case 3:
break;
case 4:
break;
}
initiateFragment();
return false;
}
}
)
.build();
#Override
public void onBackPressed() {
int count = getFragmentManager().getBackStackEntryCount();
//handle the back press :D close the drawer first and if the drawer is closed close the activity
if (result != null && result.isDrawerOpen()) {
result.closeDrawer();
}
else if (count == 0) {
super.onBackPressed();
//additional code
}
else {
getFragmentManager().popBackStack();
}
}
private void initiateFragment(){
if (fragment != null) {
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.addToBackStack(null);
ft.replace(R.id.container_body, fragment);
ft.commit();
}
}
private void clearStack(){
int count = getFragmentManager().getBackStackEntryCount();
while(count > 0){
getFragmentManager().popBackStack();
count--;
}
}
Any help would be highly appreciated.
Also, should i have used fragments here? Or i should have just started 4 different activities for 4 different options from navigation drawer?
Are you using the correct fragment manager to get your backstack count?
I notice in other parts of your class you're using getSupportFragmentManager instead.

How to handle FragmentTransaction (cached) hidden fragments during screen rotation?

In my main layout I have a central container which contains a fragment.
I started with FragmentTransaction.replace(...) to replace the fragment with other fragments but this recreate each fragment on each replacement, which makes the UI laggy (some fragments have complexe layout etc).
Instead of replace(), some suggest using .show() and .hide() to "cache" rendered fragments, and which leads to the code below:
private HashMap<String, Fragment> mCachedFragments = new HashMap<String, Fragment>();
private Fragment mCurrentFragment = null;
#Override
public void onNavigationDrawerItemSelected(int position) {
if (position == mCurrentSectionID)
return;
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left);
if (mCurrentFragment != null) {
Log.d("TRANSACTION", "putting fragment "+mCurrentFragment.getClass().getName());
mCachedFragments.put(mCurrentFragment.getClass().getName(), mCurrentFragment);
transaction.hide(mCurrentFragment);
}
Log.d("Nav", "selected: " + position);
Fragment dest = null;
switch (position) {
case BitsListFragment.FRAGMENT_ID:
dest = mCachedFragments.get(BitsListFragment.class.getName());
if (dest == null) {
dest = BitsListFragment.newInstance();
transaction
.add(R.id.container, dest);
} else {
Log.d("TRANSACTION", "bitlist retrieved");
transaction.show(dest);
}
onSectionAttached(BitsListFragment.FRAGMENT_ID);
break;
case AchievementsFragment.FRAGMENT_ID:
dest = mCachedFragments.get(AchievementsFragment.class.getName());
if (dest == null) {
dest = AchievementsFragment.newInstance();
transaction
.add(R.id.container, dest);
} else {
Log.d("TRANSACTION", "achievements retrieved");
transaction.show(dest);
}
onSectionAttached(AchievementsFragment.FRAGMENT_ID);
break;
case StatsFragment.FRAGMENT_ID:
dest = mCachedFragments.get(StatsFragment.class.getName());
if (dest == null) {
dest = StatsFragment.newInstance();
transaction
.add(R.id.container, dest);
} else {
Log.d("TRANSACTION", "stats retrieved");
transaction.show(dest);
}
onSectionAttached(StatsFragment.FRAGMENT_ID);
break;
}
mCurrentFragment = dest;
transaction.commit();
}
The code here works perfectly as long as the Activity does not gets recreated. The fragments gets retrieved and shown, and currentFragment is hidden when transition takes place.
As you can see, I'm basically keeping a reference to the CurrentFragment and a hashmap of all the cached fragments. The problem is that when I rotation the screen, the MainActivity gets recreated and these references are reset to null or emptied. Which ends up with BitsListFragment getting created multiple times and shown on screen overlapped.
Is there a better way to cache fragments/replace fragments without recreating them?
Thanks!
Actually found another way to do this: instead of keeping an reference on my own, we can add the fragment to the backstack and then retrieve it using corresponding tag. This let's fragmentManager manages the caching by itself. And the second time you access a fragment, it doesn't gets recreated.
The only hack that you should watch out is to override onBackPressed() so that you don't return to last fragment if you don't need to.
#Override
public void onNavigationDrawerItemSelected(int position) {
if (position == mCurrentSectionID)
return;
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left);
Fragment dest;
switch (position) {
case BitsListFragment.FRAGMENT_ID:
dest = fragmentManager.findFragmentByTag(BitsListFragment.class.getName());
if (dest == null) {
Log.d("TRANSACTION", "instanciating new fragment");
dest = BitsListFragment.newInstance();
}
transaction.replace(R.id.container, dest, BitsListFragment.class.getName());
transaction.addToBackStack(BitsListFragment.class.getName());
break;
case AchievementsFragment.FRAGMENT_ID:
dest = fragmentManager.findFragmentByTag(AchievementsFragment.class.getName());
if (dest == null) {
Log.d("TRANSACTION", "instanciating new fragment");
dest = AchievementsFragment.newInstance();
}
transaction.replace(R.id.container, dest, AchievementsFragment.class.getName());
transaction.addToBackStack(AchievementsFragment.class.getName());
break;
case StatsFragment.FRAGMENT_ID:
dest = fragmentManager.findFragmentByTag(StatsFragment.class.getName());
if (dest == null) {
Log.d("TRANSACTION", "instanciating new fragment");
dest = StatsFragment.newInstance();
}
transaction.replace(R.id.container, dest, StatsFragment.class.getName());
transaction.addToBackStack(StatsFragment.class.getName());
break;
}
transaction.commit();
onSectionAttached(position);
}

how to preserve fragments recreation

I'm trying to implement a tab bar with fragments and RadioGroup
i switch fragments like on checked Change of radiogroup like this (saw something like this in sdk examples)
publi
void onCheckedChanged(RadioGroup radioGroup, int id) {
TabInfo newTab = mContent.get(id);
if (newTab != lastTab) {
FragmentTransaction transaction = mActivity.getSupportFragmentManager().beginTransaction();
if (lastTab != null && lastTab.fragment != null) {
transaction.detach(lastTab.fragment);
}
if (newTab.fragment == null) {
newTab.fragment = Fragment.instantiate(mActivity, newTab.getTag());
transaction.add(mContainerId, newTab.fragment);
} else {
transaction.attach(newTab.fragment);
}
lastTab = newTab;
transaction.setCustomAnimations(R.anim.tab_transaction, R.anim.tab_transaction);
transaction.commit();
}
}
but every time this happen attached fragment is created from scratch i.e. called onCreate and so on..
is there any way to preserve fragments from creating over and over again within an activity?
also i don't want the back button could switch fragments back;
Instead of using the methods FragmentTransaction.attach() and FragmentTransaction.detach() you could use FragmentTransaction.show() and FragmentTransaction.hide(). You would need to also alter some of the surrounding code you gave in the example above but I'll leave that as an exercise for your good-self.

Categories

Resources