Reputation: 1464
I'm populating fragments in a tablayout. I have a method in one of my fragments, and i want to call it from an activity. But when i tried to have reference of the fragment by doing like this Fragment myFragment = (Fragment ) getSupportFragmentManager().findFragmentById(R.id.my_fragment)
, myFragment is null. I'm new to android. (Sorry for the bad english)
My code so far.
public class DashboardActivity extends AppCompatActivity {
private ViewPager pager;
private TabLayout tabLayout;
private Toolbar dashboardToolbar;
public static int position;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dashboard_activity);
HomeFragment myFragment = new HomeFragment ();
if(getSupportFragmentManager().findFragmentById(R.id.homeFragment) == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.homeFragment, myFragment).commit();
}
pager = (ViewPager) findViewById(R.id.view_pager);
setupViewPager(pager);
tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.setupWithViewPager(pager);
pager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
// Toast.makeText(DashboardActivity.this, "tabSelected: " + tab.getText()+" "+ tab.getPosition(), Toast.LENGTH_SHORT).show();
// no where in the code it is defined what will happen when tab is tapped/selected by the user
// this is why the following line is necessary
// we need to manually set the correct fragment when a tab is selected/tapped
// and this is the problem in your code
pager.setCurrentItem(tab.getPosition());
position = tab.getPosition();
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
// Toast.makeText(DashboardActivity.this, "tabReSelected: " + tab.getText(), Toast.LENGTH_SHORT).show();
position = tab.getPosition();
// Reload your recyclerView here
}
});
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new HomeFragment(), "FOR YOU");
adapter.addFragment(new NotificationFragment(), "NOTIF");
adapter.addFragment(new ChatFragment(), "CHAT");
adapter.addFragment(new ProfileFragment(), "PROFILE");
viewPager.setAdapter(adapter);
}
}
Fragment
public class HomeFragment extends Fragment {
// Objects Declaration
public HomeFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.homeFragment, container, false);
}
public void myMethod(){ //method to be called
//do something
}
}
Activity
public class MyActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity_filter);
HomeFragment myFragment= (HomeFragment ) getSupportFragmentManager().findFragmentById(R.id.homeFragment);
if(myFragment!= null) {
Toast.makeText(ActivityFilter.this, "Not null.", Toast.LENGTH_SHORT).show();
home.myMethod(); // this line is not accessed since myFragment is null
}else{
Toast.makeText(ActivityFilter.this, "Null fragment.", Toast.LENGTH_SHORT).show();
}
}
}
Upvotes: 0
Views: 4687
Reputation: 157
You should use a Callback.
Create a public interface in your fragment.
public interface iCommunicateListener{
void communicate(String msg);
}
You also have to make the activity your listener. (You can have many listeners, but fragments are supposed to be reusable, so if you have many listeners it will not be as reusable as it could and should be)
private iCommunicateListener listener;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception
try {
listener = (iCommunicateListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement iCommunicateListener");
}
}
Now you have active listener.
In your onClicks or whenever you want send information to your activity, you have to call listener.communicate("doSomething");
Your Activity must implement the iCommunicateListener.
After implementation of the method communicate you can choose your logic for the different Strings or whatever you want to send through the callback. There are many other ways for communication between Activities and Fragments, but since you are just beggining learn this one. After you implement it and you see the result you can take a look at this library which will definately help you in your android development. EventBus
With EventBus you will not have to use the callbacks which will make your fragments even more reusable and flexible, but first learn the normal Callbacks. It is a basic pattern and you will use it in many different situations. More on Fragments: Fragments Hope this helps!
Upvotes: 1