Fragment best practice - Android - android

I am struggling with the structure of the fragment, Thing is... In Activity there is two fragments. One contains a list. Call this FragmentA. The other contains detail. Call this FragmentB.
With every list item in FragmentA there is a different view for FragmentB, so what is the preferred way to handle this kind of scenario?
Thank You

Without seeing the complexity of the app in question, I would suggest that each different view for FragmentB be represented in its own fragment.
The use the Fragment Transaction method to replace the placeholder (let's call this R.id.fragment_container) where FragmentB is with the appropriate fragment depending on your selection in FragmentA. Something like this:
// 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();

Related

Showing two different activites on same fragment

Guys i searched a lot but didn't got. I have two fragment
1) Form Transaction
2) Form Transaction statusin Form Transaction there are some fields to fill information and submit button. On Clicking submit i need to browse a file (which is on another activity but same fragment i.e Form Transaction). How can it be possible to have two activities on same fragment
Do i need to create another activity? And to whom will it extend?
first fragment with personal information
A Fragment belongs to a host Activity and not the other way round. An Activity can host multiple Fragments.
Reads the docs for more info:
https://developer.android.com/guide/components/fragments.html
In your case what it seems you are trying to achieve is to replace Form Transaction Fragment with a different layout and logic. You can replace it with another new Fragment itself.
Use a FragmentManager to replace the existing Fragment:
FragmentManager fm = getFragmentManager();
if (fm != null) {
// Perform the FragmentTransaction to replace the Form Transaction content.
// Using FragmentTransaction#replace will destroy any Fragments
// currently inside R.id.fragment_content and add the new Fragment
// in its place.
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.fragment_content, new YourFragment());
ft.commit();
}
Change R.id.fragment_content to your Form Transaction Fragment's placeholder and YourFragment to your newly created Fragment.

FragmentTransaction add() behavior

Working with fragments I've always used replace() for my transactions, but I wish I didn't have to save instance states anymore to restore a fragment's view and prevent reloading when coming back to that fragment. So, I've decided to work with add(). The thing is when I add another fragment, the previous fragment view remains in the background and that's fine (that's the behavior I expected), but the problem is I can actually interact with the views in the background. Example:
Fragment A has a Button
Fragment B has a TextView
When I add Fragment A and later add Fragment B, I'm able to click on Fragment A's Button, even staying on Fragment B's view.
I'm using:
FragmentTransaction fragmentTransaction =
getSupportFragmentManager().beginTransaction().
add(getRootViewContainer(),fragment,fragment.getClass().getSimpleName());
if (shouldGoBack)
fragmentTransaction.addToBackStack(fragment.getClass().getSimpleName());
where getRootViewContainer() returns the id of the FrameLayout I'm using as my activity main container.
Now, is it really the default behavior of add()?
If so, is there a proper way to avoid this or one just has to use replace()?
What you can do here is just hide previous fragment at the time of transaction of current fragment.
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
Fragment newFragment= new MyFragment ();
ft.hide(CurrentFragment.this);
ft.show(newFragment);
ft.commit();
It worked for me just try it.
FragmentTransaction.hide(fragmentBehind); //works for me!
example :
//I have it globally available
FragmentTransaction trans = MainActivity.getManager().beginTransaction();
//not globally
FragmentTransaction trans = getFragmentManager().beginTransaction();
MapFragment newFragment = new newFragment();
trans.add(R.id.fragmentContainer, newFragment, tag);
trans.hide(this);
trans.addToBackStack(tag);
trans.commit();
Yes, this is a default behaviour of add().
If you really don't want to user replace(), you can try to disable views which are inside "old" fragment.

How to recreate fragment without adding its previous state

i want to recreate or refreash a fragment without adding previuos state.I searched many thing but not find any appropriate method.please give me some suggestion.......
Your best bet is FragmentTransaction.replace().
E.g.:
// 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();
However, remember that you can only add a single instance of a Fragment class once. Trying to add the same instance again will result in a Fragment already added exception. To resolve this, either get a new instance of the Fragment class you need to add using MyFragmentClass.instantiate() or new MyFragmentClass().

Is it OK to addToBackStack and replace in a fragment transaction?

Any thoughts on the following code? In my testing I've found the replaced fragment isn't destroyed and the instance is still around when popping the back stack. Just looking to verify that this is a valid way to use fragment transactions.
getSupportFragmentManager().beginTransaction().addToBackStack(null).replace(frame, fragmentB).commit();
My reason for using replace is that it causes the replaced fragment to run it's exit animation.
You can refer to the android designer guide for fragment transaction:
http://developer.android.com/guide/components/fragments.html
Specificly the snippet below:
// 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();
So yes, what you are doing is the correct approach in replacing fragments.

Cleaning fragment

Is there any way to clean up fragments' state?
I mean such situation:
I create a fragment instance (lets call it A) passing it an ID to load some content, than return to previous fragment (B for example). And when I go to A again, passing NO ID (it should than load default data) - it already has an ID from previous usage and my logic breakes.
Do not add your 'A' to backstack. So it will not be saved into backstack. Whenever you go back and come again it will not be added. Is this what you want right.
// Instantiate a new fragment.
Fragment newFragment = CountingFragment.newInstance(mStackLevel);
// Add the fragment to the activity, pushing this transaction
// on to the back stack.
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.simple_fragment, newFragment);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
//ft.addToBackStack(null);// Do not add to backstack here.(add every time new).
ft.commit();

Categories

Resources