I am using SherlockFragmentActivity with two fragments.
I want to call methods defined in the fragments from my SherlockFragmentActivity.
Fragment1 extends Fragment{
protected void resetSettings(){
//my code
}
}
and the activity
Main extends SherlockFragmentActivity{
//here i want to call resetSettings()
}
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 am trying to set up the action bar in an Activity which extends fragment:
public class ItemDescriptionActivity extends FragmentActivity
and this is the fragment:
public class ImageSliderFragment extends Fragment
How can I set it up, I cannot extend AppCompatActivity, thanks for your help
use AppCompatActivity . AppCompatActivity extends FragmentActivity so you can you fragments inside AppCompatActivity.
The class AppCompatActivity is a child class of FragmentActivity. so you can use AppCompatActivity instead of FragmentActivity.
I have a View pager Fragment called SalesPartsFragmentHolder which has two child tabbed fragments
SalesPartsFragment
SelectedSalesPartsFragment
Both of these contains Lists with an adapter. What I want to happen is when an item is clicked on SalesPartsFragment, it is added to SelectedSalesPartsFragment. So, when a user moves to the other tab it is showing. I have tried
mAppSectionsPagerAdapter.notifyDataSetChanged();
but this is too slow, if it is possible to tell the list adapter in SelectedSalesPartsFragment or to get mAppSectionsPagerAdapter.notifyDataSetChanged(); to run on the click of a list item from within SalesPartsFragment would be great. Any ideas?/ thoughts?
You need create interface in SalesPartsFragment. Example
class SalesPartsFragment extends Fragment{
...
public interface onSalesPartsFragmentListener{
public void onItemClick()
}
private onSalesPartsFragmentListener mListener;
...
#Ovveride
public void onAttach(Activity activity){
mListener = (onSalesPartsFragmentListener)activity;
}
...
void onItemClick(){
mListener.onItemClick();
}
Activity must implements onSalesPartsFragmentListener and notify SelectedSalesPartsFragment when SelectedSalesFragment was clicked.
class MainActivity extends Activity implements onSalesPartsFragmentListener{
...
public void onItemClick(){
selectedSalesPartsFragment.update()
}
In SelectedSalesPartsFragment create method update() and call notifyDataSetChanged();
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
I have implemented:
public class CategoryListFragment extends ListFragment
(Categories) Tab1
public class CrimeListFragment extends ListFragment
(List of crimes) Tab2
-- If you click on the tab it returns ALL crimes.
public class CrimeFragment extends Fragment
(Individual Crime) Tab 3
-- If you click on the tab it returns first crime.
And the activity that hosts all of the tabs:
public class TabsFragmentActivity extends FragmentActivity implements TabHost.OnTabChangeListener
What I want to implement is an onListItemClick on (Tab 1) "Categories" that passes the ID or string to Tab 2 so that only the crimes of that category are displayed.
This is already described in the official android documentation.
please follow the link and read about it there: https://developer.android.com/training/basics/fragments/communicating.html