How to use FragmentLayout without extending Fragment class - android

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.

Related

how to call function from other fragment class

I'm trying to call this function public void arrowClick()
is inside my main fragment public class CounterMain extends Fragment implements View.OnClickListener{
the fragment is extend android.support.v4.app.Fragment
I want to call this function from another Custmoe Dialog fragment
public class CustmoeDialog extends DialogFragment {
I tried ((CounterMain)getActivity()).arrowClick();
but I can't use it it says android.support.v4.app.Fragment cannot be cast to my.example.CounterMain
and the
CounterMain x = new CounterMain(); x.arrowClick();
it makes my app to stop working when I call it
any help?
You can call activity method by this way
((YourActivityClassName)getActivity()).yourPublicMethod();
and from activity you can directly call by this way
FragmentManager fm = getSupportFragmentManager();//if added by xml
YourFragmentClass fragment = (YourFragmentClass)fm.findFragmentById(R.id.your_fragment_id);
fragment.yourPublicMethod();
if you added fragment via code and used a tag string when you added your fragment, use findFragmentByTag instead:
YourFragmentClass fragment = (YourFragmentClass)fm.findFragmentByTag("yourTag");
First of all, you can not cast from ACTIVITY to FRAGMENT
But to cast from GLOBAL ACTIVITY to your ACTIVITY
Or create INTERFACE and implement it in ACTIVITY
Then cast it in FRAGMENT from ACTIVITY to INTERFACE
And on the question:
See here the right way how to implement
Basic communication between two fragments

Trying to start a Fragment from an Activity.

I have a tabbed view which I have implemented using Action Bar tabs, now there are one or two pages which navigate away from this tab view. At some point of time I want to call one of the fragments in the foreground again. But I am not finding any example of how to do this.
Class Definition(Fragment1_2):
public class Fragment1_2 extends Fragment {
Class Definition(AdhocEdit.class):
public class AdhocEdit extends Activity{
Activity to Fragment Intent so far:
Fragment1_2 fragmentB = (Fragment1_2)getFragmentManager().findFragmentById(R.id.fragemnt1_2);
/* Intent mainIntent;
mainIntent = new Intent(AdhocEdit.this,Fragment1_2.class);
AdhocEdit.this.startActivity(mainIntent);
AdhocEdit.this.finish(); */
// startActivity(new Intent(AdhocEdit.this, Fragment1_2.class));
Commented because none of them works. Also do I need to add this Fragment1_2 into the Manifest, if so how?
You can't start a fragment like you do for activity.
Fragment is hosted by a activity. You need to add the fragment to the container.
Example from docs
ExampleFragment fragment = new ExampleFragment();
fragmentTransaction.add(R.id.fragment_container, fragment);
fragmentTransaction.commit();
fragment_container is the id of the container which is usually a FrameLayout and you add the desired fragment the container
More info #
http://developer.android.com/guide/components/fragments.html

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.

Android navigating to parent Fragment Activity from within Fragments

The main fragment activity in my application has the following function
private final void insertFragmentIntoView(final SherlockFragment fragment,
String tag) {
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.fragment_content, fragment, tag);
ft.commit();
}
The R.id.fragment_content is a frame layout and i basically insert a new fragment into this frame. Now the frame that i insert has a button that should take me onto a new screen. What i want is that all navaigation in my application should take place in my FragmentActivity. How can i call my FragmentActivity from with a child fragment ?
Kind Regards
Make a listener class in your fragment and your parent activity will implement that listener .
Now register listener in your fragment and call method in which you want to perform some action.
More you can see this link http://developer.android.com/training/basics/fragments/communicating.html
Example how fragment and activity communicate.
class MyFragment extends Fragment{
class interface MyFragmentListener {
doSomeAction();
}
MyFragmentListener myListener;
onAttach(){
myListener=(MainActivity )getActivity();
}
onButtonClick(){
myListener.doSomeAction();
}
}
class MainActivity extends FragmentActivity implements MyFragment.MyFragmentListener
{
doSomeAction(){
//TODO perform some action from your fragment to activity
}
}
Using listeners is the recommended way of communicating between Fragment and your activity.
See this Android documentation section for infromation. Long story short they just implement a listener interface by the Activity class and cast getActivity() result in a fragment to a listener.
From my personal experience this is very convenient because lets you to:
Easilly switch underlying activity (e.g. you host entire fragment in a wrapper activity for compatibility in pre-3.0 and host this fragment along with others in 11+)
Easilly control if the wrapper activity supports callbacks or not. Just check is it does implement the listener and do your app specific actions if it doesn't.

issue with getFragmentManager().beginTransaction()

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.

Categories

Resources