update show/hide button

A Fragment saves its hidden state. Update the text of the button
accodring to state of the Fragment.

Bug: 22666353
This commit is contained in:
Todd Kennedy
2015-07-23 10:06:18 -07:00
parent 15688b61f2
commit 5383dc24fc
2 changed files with 16 additions and 4 deletions

View File

@@ -43,8 +43,14 @@ public class FragmentHideShow extends Activity {
// The content view embeds two fragments; now retrieve them and attach
// their "hide" button.
FragmentManager fm = getFragmentManager();
addShowHideListener(R.id.frag1hide, fm.findFragmentById(R.id.fragment1));
addShowHideListener(R.id.frag2hide, fm.findFragmentById(R.id.fragment2));
Fragment fragment1 = fm.findFragmentById(R.id.fragment1);
addShowHideListener(R.id.frag1hide, fragment1);
final Button button1 = (Button)findViewById(R.id.frag1hide);
button1.setText(fragment1.isHidden() ? "Show" : "Hide");
Fragment fragment2 = fm.findFragmentById(R.id.fragment2);
addShowHideListener(R.id.frag2hide, fragment2);
final Button button2 = (Button)findViewById(R.id.frag2hide);
button2.setText(fragment2.isHidden() ? "Show" : "Hide");
}
void addShowHideListener(int buttonId, final Fragment fragment) {

View File

@@ -44,8 +44,14 @@ public class FragmentHideShowSupport extends FragmentActivity {
// The content view embeds two fragments; now retrieve them and attach
// their "hide" button.
FragmentManager fm = getSupportFragmentManager();
addShowHideListener(R.id.frag1hide, fm.findFragmentById(R.id.fragment1));
addShowHideListener(R.id.frag2hide, fm.findFragmentById(R.id.fragment2));
Fragment fragment1 = fm.findFragmentById(R.id.fragment1);
addShowHideListener(R.id.frag1hide, fragment1);
final Button button1 = (Button)findViewById(R.id.frag1hide);
button1.setText(fragment1.isHidden() ? "Show" : "Hide");
Fragment fragment2 = fm.findFragmentById(R.id.fragment2);
addShowHideListener(R.id.frag2hide, fragment2);
final Button button2 = (Button)findViewById(R.id.frag2hide);
button2.setText(fragment2.isHidden() ? "Show" : "Hide");
}
void addShowHideListener(int buttonId, final Fragment fragment) {