I'm trying to change select item from navigation drawer when click button in a fragment
I'have a MainActivity that have a navigation drawer with 3 item..
When the app initialize, the first item is obviously selected...
When click on second item, the second item obviously is selected and start a fragment..
This is my code...
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_home) {
FragmentExample fragment = new FragmentExample();
android.support.v4.app.FragmentTransaction fragmentTransaction =
getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.commit();
The fragment have a button that when clicked should select a specific item..
How can i change select item of navigation drawer ( situated in MainActivity) when click a button in fragment??
Sorry for my english.. =)
You should define an interface in your fragment, and have your activity implement that interface. You would then call the interface method when you want to trigger the action.
public interface MyInterface {
void doSometing();
}
public void onAttach(Activity activity) {
super.onAttach(activity);
if (activity instanceof MyInterface) {
mCallback = (MyInterface) activity;
} else {
throw new ClassCastException(activity + " must implement MyInterface");
}
}
@Override
public void onDetach() {
mCallback = null;
super.onDetach();
}
Then you simply call
mCallback.doSometing();
to call into your activity.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments