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
Related
I have implemented fragment in viewpager and Fragment has some buttons. Also viewpager is in activity_main layout.
I want that when button is clicked then it implement a method which is mentioned in mainActivity.java.
How can I do this?
I am a beginner.
You can get MainActivity method from fragment like below.
Make sure your methid is public if MainActivity and ViewPager are not in same package.
((MainActivity) getActivity()).getMethodOfMain();
You can call getActivity Method from your fragment and cast it to the your respective Activity.
lets take an example : You Have MainActivity class
MainActivity {
public void check() {}
}
And you have fragment : MainFragment
MainFragment{
Activity mainActivity = (MainActivity) getActivity();
mainActivity.check();
}
Thats how you can call the Respective Activity Method.
i got a problem while working with fragments, in the first fragment i got a textview, ie:
<EditText
android:id="#+id/LeyOrdenanza"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="#dimen/dpde10"
android:ems="10"
android:hint="Ley / Ordenanza"
android:textColor="#color/cafeoscuro"
android:textColorHint="#color/cafeclaro"
android:textSize="#dimen/legra25" />
the user puts a value, then i change of fragmet, in the new fragment i got a button that calls a function in the class that contains the fragments like this :
EditText LeyOrdenanzav = (EditText) findViewById(R.id.LeyOrdenanza);
LeyOrdenanza = LeyOrdenanzav.getText().toString();
but i get null, when i put the button with the function on the same fragment, everything works like a charm, but i need to change that button to the second fragment, thats when it stops working, any suggestions?...
Hi i hope i got your question right,
in order to send data between fragments you need to create an interface communicator as follows, I'll suggest to make a new class so not to confuse yourself.
public interface Communicator {
public void getData(String data);
}
Have your MainActivity Implement Communicator as follows.
public class MainActivity extends ActionBarActivity implements Communicator
after implementing the communicator add the umimplemented method which is getData in the form String in your Case.
this method will work as a hub between the two fragments.
Create an Instance of the Communicator in the fragment where you get the String from the TextView
Communicator communicator = (Communicator) getActivity();
Now Pass the String Like this communicator.getData(StringtToPass);
Initialize an instance of the Fragment in the method using Fragment Manger
FragmentManager fragmentManager = getFragmentManager(); fragmentTwo fragmentTwo = (fragmentTwo) fragmentManager.findFragmentById(R.id.fragmentTwo);
On the Other fragment create a method similar to this
public void setData(String string) {
textView.setText(stringPassed);
}
After you initialize the Fragment in the MainActivity you can pass the data this way.
fragmentTwo.setData(StringToPass);
Hope this will help a little.
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.
I have a problem and I am stuck as to how to implement the following structure:
class A extends FragmentActivity{
//In this I click a button which loads a fragment say class B
}
class B extends Fragment implments Interface{
//This has a button which opens up an activity of list view say class C
function replaceFragment(){
try{
FragmentManager fm = B.this.getFragmentManager();
fm.beginTransaction().replace(R.id.main_frame, new D()).commit();
}catch(Exception e){
Log.v("error",""+e.getMessage());
}
}
}
class C extends Activity{
//It has an async task which loads something when the list view is clicked
//The async task onPostExecute calls back the class B interface function
onPostExecute(){
callback.replaceFragment();
}
}
Now when the interface function is called I want to replace the fragment B with another fragment say D which has its own layout. Can anyone help me in giving this suggestion or the solution to the problem I am having?
Another method I thought was instead of calling the callback function I would start class A all over again and changing the fragment to D when it gets loaded, but then the previous class A wasn't removed off the stack and when I press back I get the class A fragment activity again and this is causing a problem. Need a solution as I am stuck badly in this.
When interface function is called, you can replace the existing Fragment with Fragment D as follows
Fragment d = new D();
fragmentManager.beginTransaction().replace(R.id.frame_container, d).commit();
R.id.frame_container1 is the ID of the FrameLayout where you add/replace youre fragments
You can try calling replace in your interface call back. I think this should work well.
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.