Last active
August 29, 2015 13:57
-
-
Save pipiscrew/9495623 to your computer and use it in GitHub Desktop.
[android] ViewPager - Find Active Fragment And Execute Method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//main_activity_super.xml | |
<menu xmlns:android="http://schemas.android.com/apk/res/android" > | |
<item android:id="@+id/logout" | |
android:title="logout" | |
android:showAsAction="never"/> | |
<item android:id="@+id/refresh" | |
android:title="refresh" | |
android:showAsAction="never"/> | |
<!-- http://stackoverflow.com/questions/8312344/how-to-add-a-dropdown-item-on-the-action-bar --> | |
</menu> | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
// getActionBar().setDisplayShowHomeEnabled(false); | |
// Inflate the menu items for use in the action bar | |
getActionBar().setTitle("Contests Admins"); | |
getActionBar().setDisplayUseLogoEnabled(false); | |
MenuInflater inflater = getMenuInflater(); | |
inflater.inflate(R.menu.main_activity_super, menu); | |
return super.onCreateOptionsMenu(menu); | |
} | |
@Override | |
public boolean onOptionsItemSelected(MenuItem item) { | |
// Handle item selection | |
switch (item.getItemId()) { | |
case R.id.logout: | |
logoutADMIN(); | |
return true; | |
case R.id.refresh: | |
//get active fragment (int) | |
switch (mViewPager.getCurrentItem()) { | |
case 0: | |
Toy1 toy1 = (Toy1) getSupportFragmentManager().findFragmentByTag( | |
"android:switcher:" + mViewPager.getId() + ":" + 0); | |
toy1.queryDB1(); //call method inside fragment | |
case 1: | |
Toy2 toy2 = (Toy2) getSupportFragmentManager().findFragmentByTag( | |
"android:switcher:" + mViewPager.getId() + ":" + 1); | |
toy2.queryDB2(); //call method inside fragment | |
} | |
return true; | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment