Showing two different activites on same fragment - android

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.

Related

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.

Android activity and fragments

I've setup a new Android project that comes with an activity. Here's the boiler plate code:
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
Can someone explain what this is doing exactly? From what I can see, it checks if the activity hasn't been initliazed and then inflates the layout. But what I don't understand is beginTransaction(), ew PlaceholderFragment(), and commit()
Thanks.
You use fragment transactions to add / replace (etc) fragments within a FrameLayout (R.id.container) and new PlaceholderFragment is a new instance of a fragment to be put into the container
//Check whether we're recreating a previously destroyed instance
if (savedInstanceState == null) {
//Execute a transaction, replacing any existing fragment with this one inside the frame.
//Getting FragmentManager object which will control fragment acvitiy
FragmentManager fm = getFragmentManager()
//Starting a FragmentTransaction which will handle transaction to this fragment activity
FragmentTransaction ft = fragmentManager.beginTransaction();
//Add a fragment to the activity state. This fragment may optionally also have its view (if Fragment.onCreateView returns non-null) into a container view of the activity.
ft.add(R.id.container, new PlaceholderFragment());
//Schedules a commit of this transaction.
ft.commit();
}
There is a good explanation to fragment activity here, here and here
FragmentManager is a class which helps in managing the fragments that an activity may need. So here you are basically getting an instance of it and you are beginning a transaction. You need an instance of transaction because it lets the runtime know that some change is going to happen when this is called. Here 'add()' is that change and finally you commit it to save that change.
The arguments to add are the layout where the fragment needs to be added and the PlaceHolderFragment() is the name of the Fragment you need to put in.
As fragments are the way to go, replacing all of many heavy Activites, Eclipse has also adapted to this change which cause the boiler alert. :)
Starting a fragment (which cant be done via Intents) is treated as a transaction just like in database (not a good example i guess).
getFragmentManager() - gets the Activities FragmentManger which is responsible to initiate FragmentTransaction.
beginTransaction() - creates a new Transaction for this particular fragment job.
new PlaceholderFragment() - is an instance of the PlaceholderFragment which you can find if scroll more in the Activity.
commit - a way to commit this trasaction and bring it to effect.
See Android docs. for more details. :)
It is simple my friend.
In simple Coding Language:
if (savedInstanceState == null) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.tab5, new PlaceholderFragment());
fragmentTransaction.commit();
}
If you think that it explain everything, then it is a pleasure for me. Otherwise just ping me to add theory information.

Keeping Fragment count to one

When I press a button, my app creates a fragment. I'd like to make sure that only one of this fragment is created, e.g. disable the button if the fragment already exists.
How do I check that it creates only one fragment? Is it possible to get a Fragment count or is there some option that limits it to creating only one?
You can use the methods that sandrstar mentioned. When attaching a fragment u can first check to see if its already attached.
For example if you are adding the fragment dynamically you can stop the fragment from being re-added by doing the following:
MyFragment myFragment = getFragmentManager().findFragmentByTag("MyFragmentTag")
if(myFragment == null)
{
myFragment = MyFragment.newInstance();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.add(R.layout.mylayout,myFragment,"MyFragmentTag");
ft.commit();
}

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();

Fragment best practice - 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();

Categories

Resources