Advertisement
vyoumans

Android STUDIO, replacing Fragments

Jul 15th, 2014
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.92 KB | None | 0 0
  1. I am trying to do a connect the dots kind of Navigation thing in ANDROID Studio 0.8
  2. I created this from BlankActivity with Fragment.
  3.  
  4. Fragment_My has a button that on click should goto FRAG02
  5. FRAG02 goes to FRAG03
  6.  
  7. //===============================================================================
  8. MyActivity
  9. //===============================================================================
  10.  
  11. import android.app.Activity;
  12. import android.app.ActionBar;
  13. import android.app.Fragment;
  14. import android.app.FragmentManager;
  15. import android.app.FragmentTransaction;
  16. import android.os.Bundle;
  17. import android.view.LayoutInflater;
  18. import android.view.Menu;
  19. import android.view.MenuItem;
  20. import android.view.View;
  21. import android.view.ViewGroup;
  22. import android.os.Build;
  23. import android.widget.Toast;
  24.  
  25.  
  26. public class MyActivity extends Activity {
  27.  
  28. @Override
  29. protected void onCreate(Bundle savedInstanceState) {
  30. super.onCreate(savedInstanceState);
  31. setContentView(R.layout.activity_my);
  32. if (savedInstanceState == null) {
  33. getFragmentManager().beginTransaction()
  34. .add(R.id.container, new PlaceholderFragment())
  35. //.add(R.id.container, new FRAG02())
  36. .commit();
  37. }
  38. }
  39.  
  40.  
  41. @Override
  42. public boolean onCreateOptionsMenu(Menu menu) {
  43. // Inflate the menu; this adds items to the action bar if it is present.
  44. getMenuInflater().inflate(R.menu.my, menu);
  45. return true;
  46. }
  47.  
  48. @Override
  49. public boolean onOptionsItemSelected(MenuItem item) {
  50. // Handle action bar item clicks here. The action bar will
  51. // automatically handle clicks on the Home/Up button, so long
  52. // as you specify a parent activity in AndroidManifest.xml.
  53. int id = item.getItemId();
  54. if (id == R.id.action_settings) {
  55. return true;
  56. }
  57. return super.onOptionsItemSelected(item);
  58. }
  59.  
  60. /**
  61. * A placeholder fragment containing a simple view.
  62. */
  63. public static class PlaceholderFragment extends Fragment {
  64.  
  65. public PlaceholderFragment() {
  66. }
  67.  
  68. @Override
  69. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  70. Bundle savedInstanceState) {
  71. View rootView = inflater.inflate(R.layout.fragment_my, container, false);
  72. return rootView;
  73. }
  74. }
  75.  
  76. public void btnClickGotoFrag02 (View view){
  77. Toast.makeText(getApplicationContext(), "----- just clicked goto frag02 ----", Toast.LENGTH_LONG).show();
  78.  
  79.  
  80. // this is where we chall change to a new Fragment
  81.  
  82. FragmentManager FM = getFragmentManager();
  83. FragmentTransaction FT = FM.beginTransaction();
  84. //Fragment newFrag02 = new FRAG02();
  85. FT.replace(R.id.container, new FRAG02());
  86. FT.addToBackStack(null);
  87.  
  88. FT.commit();
  89.  
  90. //mListener = (FRAG02.OnFragmentInteractionListener) MyActivity;
  91. //mListener.onFragmentInteraction(null);
  92. }
  93.  
  94.  
  95.  
  96. public void btnClickGotoFrag03 (View view){
  97. Toast.makeText(getApplicationContext(), "----- just clicked goto frag03 ----", Toast.LENGTH_LONG).show();
  98.  
  99. // this is where we chall change to a new Fragment
  100.  
  101. FragmentManager FM = getFragmentManager();
  102. FragmentTransaction FT = FM.beginTransaction();
  103. //Fragment newFrag02 = new FRAG02();
  104. FT.replace(R.id.container, new FRAG03());
  105. FT.addToBackStack(null);
  106.  
  107. FT.commit();
  108.  
  109. //mListener = (FRAG03.OnFragmentInteractionListener) MyActivity;
  110. //mListener.onFragmentInteraction(null);
  111. }
  112.  
  113.  
  114. }
  115.  
  116.  
  117.  
  118. //===============================================================================
  119. FRAG02
  120. //===============================================================================
  121.  
  122. package com.techlatin.vyoumans.blankfrag31;
  123.  
  124. import android.app.Activity;
  125. import android.net.Uri;
  126. import android.os.Bundle;
  127. import android.app.Fragment;
  128. import android.view.LayoutInflater;
  129. import android.view.View;
  130. import android.view.ViewGroup;
  131.  
  132.  
  133.  
  134. /**
  135. * A simple {@link Fragment} subclass.
  136. * Activities that contain this fragment must implement the
  137. * {@link FRAG02.OnFragmentInteractionListener} interface
  138. * to handle interaction events.
  139. * Use the {@link FRAG02#newInstance} factory method to
  140. * create an instance of this fragment.
  141. *
  142. */
  143. public class FRAG02 extends Fragment {
  144. // TODO: Rename parameter arguments, choose names that match
  145. // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
  146. private static final String ARG_PARAM1 = "param1";
  147. private static final String ARG_PARAM2 = "param2";
  148.  
  149. // TODO: Rename and change types of parameters
  150. private String mParam1;
  151. private String mParam2;
  152.  
  153. private OnFragmentInteractionListener mListener;
  154.  
  155. /**
  156. * Use this factory method to create a new instance of
  157. * this fragment using the provided parameters.
  158. *
  159. * @param param1 Parameter 1.
  160. * @param param2 Parameter 2.
  161. * @return A new instance of fragment FRAG02.
  162. */
  163. // TODO: Rename and change types and number of parameters
  164. public static FRAG02 newInstance(String param1, String param2) {
  165. FRAG02 fragment = new FRAG02();
  166. Bundle args = new Bundle();
  167. args.putString(ARG_PARAM1, param1);
  168. args.putString(ARG_PARAM2, param2);
  169. fragment.setArguments(args);
  170. return fragment;
  171. }
  172. public FRAG02() {
  173. // Required empty public constructor
  174. }
  175.  
  176. @Override
  177. public void onCreate(Bundle savedInstanceState) {
  178. super.onCreate(savedInstanceState);
  179. if (getArguments() != null) {
  180. mParam1 = getArguments().getString(ARG_PARAM1);
  181. mParam2 = getArguments().getString(ARG_PARAM2);
  182. }
  183. }
  184.  
  185. @Override
  186. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  187. Bundle savedInstanceState) {
  188. // Inflate the layout for this fragment
  189. return inflater.inflate(R.layout.fragment_frag02, container, false);
  190. }
  191.  
  192. // TODO: Rename method, update argument and hook method into UI event
  193. public void onButtonPressed(Uri uri) {
  194. if (mListener != null) {
  195. mListener.onFragmentInteraction(uri);
  196. }
  197. }
  198.  
  199. @Override
  200. public void onAttach(Activity activity) {
  201. super.onAttach(activity);
  202. try {
  203. mListener = (OnFragmentInteractionListener) activity;
  204. } catch (ClassCastException e) {
  205. throw new ClassCastException(activity.toString()
  206. + " must implement OnFragmentInteractionListener");
  207. }
  208. }
  209.  
  210. @Override
  211. public void onDetach() {
  212. super.onDetach();
  213. mListener = null;
  214. }
  215.  
  216. /**
  217. * This interface must be implemented by activities that contain this
  218. * fragment to allow an interaction in this fragment to be communicated
  219. * to the activity and potentially other fragments contained in that
  220. * activity.
  221. * <p>
  222. * See the Android Training lesson <a href=
  223. * "http://developer.android.com/training/basics/fragments/communicating.html"
  224. * >Communicating with Other Fragments</a> for more information.
  225. */
  226. public interface OnFragmentInteractionListener {
  227. // TODO: Update argument type and name
  228. public void onFragmentInteraction(Uri uri);
  229. }
  230.  
  231. }
  232.  
  233.  
  234.  
  235.  
  236.  
  237. //===============================================================================
  238. FRAG03
  239. //===============================================================================
  240.  
  241. package com.techlatin.vyoumans.blankfrag31;
  242.  
  243. import android.app.Activity;
  244. import android.net.Uri;
  245. import android.os.Bundle;
  246. import android.app.Fragment;
  247. import android.view.LayoutInflater;
  248. import android.view.View;
  249. import android.view.ViewGroup;
  250.  
  251.  
  252.  
  253. /**
  254. * A simple {@link Fragment} subclass.
  255. * Activities that contain this fragment must implement the
  256. * {@link FRAG03.OnFragmentInteractionListener} interface
  257. * to handle interaction events.
  258. * Use the {@link FRAG03#newInstance} factory method to
  259. * create an instance of this fragment.
  260. *
  261. */
  262. public class FRAG03 extends Fragment {
  263. // TODO: Rename parameter arguments, choose names that match
  264. // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
  265. private static final String ARG_PARAM1 = "param1";
  266. private static final String ARG_PARAM2 = "param2";
  267.  
  268. // TODO: Rename and change types of parameters
  269. private String mParam1;
  270. private String mParam2;
  271.  
  272. private OnFragmentInteractionListener mListener;
  273.  
  274. /**
  275. * Use this factory method to create a new instance of
  276. * this fragment using the provided parameters.
  277. *
  278. * @param param1 Parameter 1.
  279. * @param param2 Parameter 2.
  280. * @return A new instance of fragment FRAG03.
  281. */
  282. // TODO: Rename and change types and number of parameters
  283. public static FRAG03 newInstance(String param1, String param2) {
  284. FRAG03 fragment = new FRAG03();
  285. Bundle args = new Bundle();
  286. args.putString(ARG_PARAM1, param1);
  287. args.putString(ARG_PARAM2, param2);
  288. fragment.setArguments(args);
  289. return fragment;
  290. }
  291. public FRAG03() {
  292. // Required empty public constructor
  293. }
  294.  
  295. @Override
  296. public void onCreate(Bundle savedInstanceState) {
  297. super.onCreate(savedInstanceState);
  298. if (getArguments() != null) {
  299. mParam1 = getArguments().getString(ARG_PARAM1);
  300. mParam2 = getArguments().getString(ARG_PARAM2);
  301. }
  302. }
  303.  
  304. @Override
  305. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  306. Bundle savedInstanceState) {
  307. // Inflate the layout for this fragment
  308. return inflater.inflate(R.layout.fragment_frag03, container, false);
  309. }
  310.  
  311. // TODO: Rename method, update argument and hook method into UI event
  312. public void onButtonPressed(Uri uri) {
  313. if (mListener != null) {
  314. mListener.onFragmentInteraction(uri);
  315. }
  316. }
  317.  
  318. @Override
  319. public void onAttach(Activity activity) {
  320. super.onAttach(activity);
  321. try {
  322. mListener = (OnFragmentInteractionListener) activity;
  323. } catch (ClassCastException e) {
  324. throw new ClassCastException(activity.toString()
  325. + " must implement OnFragmentInteractionListener");
  326. }
  327. }
  328.  
  329. @Override
  330. public void onDetach() {
  331. super.onDetach();
  332. mListener = null;
  333. }
  334.  
  335. /**
  336. * This interface must be implemented by activities that contain this
  337. * fragment to allow an interaction in this fragment to be communicated
  338. * to the activity and potentially other fragments contained in that
  339. * activity.
  340. * <p>
  341. * See the Android Training lesson <a href=
  342. * "http://developer.android.com/training/basics/fragments/communicating.html"
  343. * >Communicating with Other Fragments</a> for more information.
  344. */
  345. public interface OnFragmentInteractionListener {
  346. // TODO: Update argument type and name
  347. public void onFragmentInteraction(Uri uri);
  348. }
  349.  
  350. }
  351.  
  352.  
  353.  
  354.  
  355. //===============================================================================
  356. activity_my.xml
  357. //===============================================================================
  358. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  359. xmlns:tools="http://schemas.android.com/tools"
  360. android:id="@+id/container"
  361. android:layout_width="match_parent"
  362. android:layout_height="match_parent"
  363. tools:context=".MyActivity"
  364. tools:ignore="MergeRootFrame" />
  365.  
  366.  
  367. //===============================================================================
  368. fragment_my.xml
  369. //===============================================================================
  370.  
  371. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  372. xmlns:tools="http://schemas.android.com/tools"
  373. android:layout_width="match_parent"
  374. android:layout_height="match_parent"
  375. android:paddingLeft="@dimen/activity_horizontal_margin"
  376. android:paddingRight="@dimen/activity_horizontal_margin"
  377. android:paddingTop="@dimen/activity_vertical_margin"
  378. android:paddingBottom="@dimen/activity_vertical_margin"
  379. tools:context=".MyActivity$PlaceholderFragment">
  380.  
  381. <TextView
  382. android:text="@string/txtFrag01"
  383. android:layout_width="wrap_content"
  384. android:layout_height="wrap_content"
  385. android:id="@+id/txtFrag0001" />
  386.  
  387. <Button
  388. android:layout_width="fill_parent"
  389. android:layout_height="wrap_content"
  390. android:text="Goto Frag 02"
  391. android:id="@+id/btnGotoFrag02"
  392. android:layout_centerVertical="true"
  393. android:layout_alignParentLeft="true"
  394. android:layout_alignParentStart="true"
  395. android:onClick="btnClickGotoFrag02" />
  396.  
  397.  
  398. </RelativeLayout>
  399.  
  400.  
  401. //===============================================================================
  402. fragment_FRAG02.xml
  403. //===============================================================================
  404.  
  405. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  406. xmlns:tools="http://schemas.android.com/tools"
  407. android:layout_width="match_parent"
  408. android:layout_height="match_parent"
  409. tools:context="com.techlatin.vyoumans.blankfrag31.FRAG02">
  410.  
  411. <!-- TODO: Update blank fragment layout -->
  412. <TextView
  413. android:layout_width="match_parent"
  414. android:layout_height="match_parent"
  415. android:text="@string/txtFrag02"
  416. android:id="@+id/FRAG00002" />
  417.  
  418. <Button
  419. android:layout_width="fill_parent"
  420. android:layout_height="wrap_content"
  421. android:text="GOTO FRAG 03"
  422. android:id="@+id/btnGotoFRAG03"
  423. android:layout_gravity="left|center_vertical"
  424. android:onClick="btnClickGotoFrag03" />
  425.  
  426. </FrameLayout>
  427.  
  428.  
  429.  
  430.  
  431. //===============================================================================
  432. fragment_FRAG03.xml
  433. //===============================================================================
  434.  
  435.  
  436. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  437. xmlns:tools="http://schemas.android.com/tools"
  438. android:layout_width="match_parent"
  439. android:layout_height="match_parent"
  440. tools:context="com.techlatin.vyoumans.blankfrag31.FRAG02">
  441.  
  442. <!-- TODO: Update blank fragment layout -->
  443. <TextView
  444. android:layout_width="match_parent"
  445. android:layout_height="match_parent"
  446. android:text="@string/txtFrag02"
  447. android:id="@+id/FRAG00002" />
  448.  
  449. <Button
  450. android:layout_width="fill_parent"
  451. android:layout_height="wrap_content"
  452. android:text="GOTO FRAG 03"
  453. android:id="@+id/btnGotoFRAG03"
  454. android:layout_gravity="left|center_vertical"
  455. android:onClick="btnClickGotoFrag03" />
  456.  
  457. </FrameLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement