issue with getFragmentManager().beginTransaction() - android

I have two fragments and setting them like below
fram1 = new FragMent1();
fragMentTra.addToBackStack(null);
fragMentTra = getFragmentManager().beginTransaction().add(rl.getId(), fram1, "fram1");
fram2 = new FragMent2();
fragMentTra.addToBackStack(null);
fragMentTra = getFragmentManager().beginTransaction().add(rl.getId(), fram2, "fram2");
Fragment extends Listfragment and not the other.
public class FragMent1 extends android.support.v4.app.ListFragment {
public class FragMent2 extends Fragment{
When I perform the line fragMentTra = getFragmentManager().beginTransaction().add(rl.getId(), fram1, "fram1");
I get the below error message. Anyone know how to solve this?
The method add(int, Fragment, String) in the type FragmentTransaction is not applicable for the arguments (int, FragMent1, String)

Since you're using the support library's Fragments, your Activity will need to extend FragmentActivity, and the method to use is getSupportFragmentManager().
You might have to always use the support library for your Fragments, rather than the native implementation, until you plan on targeting only API 11 and up.

FragMent1 should extend android.app.ListFragment, not android.support.v4.app.ListFragment.

Related

Android activity with ListView open fragment on item click

I have an Activity with ListView, when clicking on a list item opens a new fragment.
Do I need to create a new fragment every time like this?
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.root_layout,new MyFragment());
Or will be enough to create a fragment once and then use it?
in activity:
MyFragment myFragment = new MyFragment();
......
in onItemClickListener:
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.root_layout,myFragment);
It depends on your case. In most cases every list item opens a different fragment (with different data). Then you have to make a static newInstance(Data mySerializableData) method in your fragment, use default constructor inside of it, pass data over Fragment arguments See DetailFragment and use fragmentTransaction.replace() in your activity to add this fragment.
When you dont want your fragment to be changed you can create it only once as you say but there is no need of adding it on every item click. So one creation and only one add.
No, you don't need to create it every time. First, instead of using "add", use "replace". If there is no fragment in fragment manager, your fragment will be added, instead it will be replaced. If you use "add", then you could accidentally add more than one fragment.
You should check for the fragment in the fragment manager and call methods for content updates.
Example:
myFragment = (MyFragment) fragmentManager.findFragmentById(R.id.my_fragment);
if (myFragment == null) {
myFragment = MyFragment.getInstance(someDataIfNeeded);
fragmentManager.beginTransaction().replace(R.id.my_fragment, myFragment).commit();
} else {
myFragment.updateFragmentContent(someData);
}
check instance of that fragment everytime like this-
In your fragment class -
public static final MyFragment newInstance()
{
MyFragment f = new MyFragment();
return f;
}
and in your activity when you want to create fragment -
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.root_layout,MyFragment.newInstance());
this is well n good manner...

How to use FragmentLayout without extending Fragment class

I am creating fragment using class from another package
//ONCREATE
PickKanjiActivity pickKanji=new PickKanjiActivity();
//SOME_METHOD
fTrans = getFragmentManager().beginTransaction();
fTrans.add(R.id.frameKanji, pickKanji);
fTrans.commit();
But this code is giving following error:
The method add(int, Fragment) in the type FragmentTransaction is not applicable for the arguments (int, PickKanjiActivity)
Then I tought that I should extend Fragment in PickKanjiActivity like this:
public class PickKanjiActivity extends Fragment
But PickKanjiActivity already extending another class
public class PickKanjiActivity extends KanjiActivity
Question is how to create fragment without extending Fragment?
You don't. Their PickKanjiActivity is an activity, not a fragment. You'd need to rewrite their code as a fragment. You can't just use it as one when it isn't one.

Fragment transaction with a FragmentActivity instead of a Fragment

I would like to add a FragmentActivity in the activity layout. In order to make fragment transactions (such as add, remove, or replace a fragment), the api guides say that I first need to get an instance of FragmentTransaction from your Activity and then add a fragment using the add() method specifying the fragment to add and the view in which to insert it. Ok pretty straightforward so far, but what should I do in the FragmentActivity case?
AllEventsFragments events;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
if ( savedInstanceState == null )
{
events = new AllEventsFragments();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.add(R.id.content_frame, events,"events");
// Commit the transaction
transaction.commit();
}
}
in which AllEventsFragments is defined as follows:
public class AllEventsFragments extends FragmentActivity implements ActionBar.TabListener
{
...
}
Since the add method accepts a Fragment as second argument the error returned is:
The method add(int, Fragment, String) in the type FragmentTransaction is not applicable for the arguments (int, AllEventsFragments, String)
I would like to add a FragmentActivity in the activity layout.
You are trying to nest activities. That is not supported via fragment transactions, and what little support there ever was for it has been deprecated for ~2.5 years.
However, you can move much of the AllEventsFragments logic into a Fragment, which can then be used from both AllEventsFragments and wherever else you are trying to use it.

Start FragmentTransaction from within ArrayAdapter

I have a ListView with several rows. Each row has a button.
I want the button to start a FragmentTransaction to replace the Fragment that the ListView is in.
However, in the getView() method of the Adapter, this line does not work:
FragmentTransaction t = getContext().getSupportFragmentManager().beginTransaction();
It does not like the context.
Can this be done in this way or does the Transaction have to happen elsewhere?
First get the context in your Constructor and then try following code,
FragmentTransaction ft = ((FragmentActivity)context).getSupportFragmentManager().beginTransaction();
getSupportFragmentManager() is only defined for the class FragmentActivity, not for Context. Thus, the compiler can't resolve the call if you try to call it on an object of type Context.
You can use reflection to do this in a safe way. This will always work as long as you pass your FragmentActivity as the Context. Note: FragmentActivity is a subclass of Context, and thus you can pass FragmentActivity wherever you can pass Context.
So use this code instead:
if (getContext() instanceof FragmentActivity) {
// We can get the fragment manager
FragmentActivity activity = (FragmentActivity(getContext()));
FragmentTransaction t = activity.getSupportFragmentManager().beginTransaction();
}
I'd suggest you to pass FragmentManager instance to the Adapter constructor like that:
public class YourAdapter extends...
private FragmentManage mFragmentManager;
public YourAdapter(FragmentManager fm) {
mFragmentManager = fm;
}
And use it explicitly:
FragmentTransaction ft = mFragmentManager.beginTransaction();
That should give you posibility to initialize Adapter with either Fragment.getFragmentManager() or FragmentActivity.getSupportFragmentManager() instance, since they are pointed at the same object

Transition to FragmentActivity

I can transition to a Fragment class using the code below, but how do i transition to a FragmentActivity, using the same code.
FragmentActivity activity = new ABC(); // ABC is a FragmentActivity
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.details, activity); // replace errors out
fragmentTransaction.commit();
You can't replace a view or a fragment with a FragmentActivity! A FragmentActivity is just an Activity and has been created for Android Compatibility support.
To summarize, an Activity can contains Fragments which can be dynamically replaced. The only way to navigate between Activities is by intent.

Categories

Resources