Advertisement
Guest User

Untitled

a guest
Oct 21st, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.14 KB | None | 0 0
  1. public class MainFragment extends Fragment {
  2.  
  3. private static final int REQUEST_CODE_DETAILS_ACTIVITY = 1234;
  4.  
  5. @Override
  6. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  7. Bundle savedInstanceState) {
  8. return inflater.inflate(R.layout.fragment_main, container, false);
  9. //return inflater.inflate(R.layout.fragment_screen2, container, false);
  10. }
  11.  
  12. @Override
  13. public void onActivityCreated(Bundle savedState) {
  14. super.onActivityCreated(savedState);
  15.  
  16. Button b1 = (Button) getActivity().findViewById(R.id.button1);
  17. b1.setOnClickListener(new View.OnClickListener() {
  18.  
  19. @Override
  20. public void onClick(View view) {
  21. onClickMethod(R.id.button1);
  22. }
  23. });
  24.  
  25. // Button b2 = (Button) getActivity().findViewById((R.id.button2);
  26. //b2.setOnClickListener(new View.);
  27. }
  28.  
  29. public void onClickMethod(int id) {
  30. // int id = R.id.button1;
  31. Toast.makeText(getActivity(), "does the id get passed?" +id , Toast.LENGTH_SHORT).show();
  32.  
  33. if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
  34. int testint = 323;
  35. FragScreen2 frag = (com.example.engg6600.fragtest2.FragScreen2) getFragmentManager().findFragmentById(R.id.fragment_screen2);
  36. //frag.changeText(id);
  37. try {
  38. ((FragScreen2)getFragmentManager().findFragmentById(R.id.fs2)).testMethod(testint);
  39. } catch (Exception e) {
  40. Toast.makeText(getActivity(), "breaks" + testint, Toast.LENGTH_SHORT).show();
  41. }
  42. //Intent goS2 = new Intent(getActivity(), Screen2.class);
  43. //goS2.putExtra("buttonNumber", id);
  44.  
  45. // FragScreen2 frag = (FragScreen2) getFragmentManager().findFragmentById(R.id.fragment_screen2);
  46. // Toast.makeText(getActivity(), "before to frag.changetext(id)", Toast.LENGTH_LONG).show();
  47. // frag.changeText(id);
  48. // Toast.makeText(getActivity(), "sent to frag.changetext(id)", Toast.LENGTH_LONG).show();
  49. } else{
  50.  
  51. // String toastString = Integer.toString(id);
  52. Toast.makeText(getActivity(), "portrait mode id" +id, Toast.LENGTH_LONG).show();
  53. Intent goS2 = new Intent(getActivity(), Screen2.class);
  54. goS2.putExtra("buttonNumber", id);
  55. startActivityForResult(goS2, REQUEST_CODE_DETAILS_ACTIVITY);
  56. }
  57. }
  58.  
  59. }
  60.  
  61. public class FragScreen2 extends Fragment {
  62.  
  63. private static final String[] SCREENTEXT ={
  64. "You have selected button 1",
  65. "You have selected button 2",
  66. "You have selected button 3",
  67. "You have selected button 4",
  68. "No selection"
  69.  
  70. };
  71.  
  72. @Override
  73. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  74. Bundle savedInstanceState) {
  75. return inflater.inflate(R.layout.fragment_screen2, container, false);
  76. }
  77.  
  78. public void onActivityCreated(Bundle savedInstanceState) {
  79. super.onActivityCreated(savedInstanceState);
  80.  
  81. // pull out the country ID to show from the activity's intent
  82. Intent intent = getActivity().getIntent();
  83. int id = intent.getIntExtra("buttonNumber", 234324);
  84. Toast.makeText(getActivity(), "fragscreen 2 id=" +id, Toast.LENGTH_LONG).show();
  85. changeText(id);
  86. }
  87.  
  88. public void changeText(int id) {
  89. int buttonNumber=4;
  90. Toast.makeText(getActivity(), "button1 of R.id" + Integer.toString(R.id.button1), Toast.LENGTH_SHORT).show();
  91. if (id==R.id.button1) {
  92. buttonNumber =0;
  93. } else if (id==R.id.button2) {
  94. buttonNumber =1;
  95. } else if (id==R.id.button3) {
  96. buttonNumber =2;
  97. } else if (id==R.id.button4) {
  98. buttonNumber =3;
  99. }
  100. String text = SCREENTEXT[buttonNumber];
  101. TextView tv = (TextView)getActivity().findViewById(R.id.textscreen2);
  102. tv.setText(text);
  103.  
  104. }
  105.  
  106. public void testMethod (int integerVar) {
  107. Toast.makeText(getActivity(), "testmethod int=" + integerVar, Toast.LENGTH_SHORT).show();
  108. }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement