Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- I am trying to do a connect the dots kind of Navigation thing in ANDROID Studio 0.8
- I created this from BlankActivity with Fragment.
- Fragment_My has a button that on click should goto FRAG02
- FRAG02 goes to FRAG03
- //===============================================================================
- MyActivity
- //===============================================================================
- import android.app.Activity;
- import android.app.ActionBar;
- import android.app.Fragment;
- import android.app.FragmentManager;
- import android.app.FragmentTransaction;
- import android.os.Bundle;
- import android.view.LayoutInflater;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.view.ViewGroup;
- import android.os.Build;
- import android.widget.Toast;
- public class MyActivity extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_my);
- if (savedInstanceState == null) {
- getFragmentManager().beginTransaction()
- .add(R.id.container, new PlaceholderFragment())
- //.add(R.id.container, new FRAG02())
- .commit();
- }
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.my, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- // Handle action bar item clicks here. The action bar will
- // automatically handle clicks on the Home/Up button, so long
- // as you specify a parent activity in AndroidManifest.xml.
- int id = item.getItemId();
- if (id == R.id.action_settings) {
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
- /**
- * A placeholder fragment containing a simple view.
- */
- public static class PlaceholderFragment extends Fragment {
- public PlaceholderFragment() {
- }
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- View rootView = inflater.inflate(R.layout.fragment_my, container, false);
- return rootView;
- }
- }
- public void btnClickGotoFrag02 (View view){
- Toast.makeText(getApplicationContext(), "----- just clicked goto frag02 ----", Toast.LENGTH_LONG).show();
- // this is where we chall change to a new Fragment
- FragmentManager FM = getFragmentManager();
- FragmentTransaction FT = FM.beginTransaction();
- //Fragment newFrag02 = new FRAG02();
- FT.replace(R.id.container, new FRAG02());
- FT.addToBackStack(null);
- FT.commit();
- //mListener = (FRAG02.OnFragmentInteractionListener) MyActivity;
- //mListener.onFragmentInteraction(null);
- }
- public void btnClickGotoFrag03 (View view){
- Toast.makeText(getApplicationContext(), "----- just clicked goto frag03 ----", Toast.LENGTH_LONG).show();
- // this is where we chall change to a new Fragment
- FragmentManager FM = getFragmentManager();
- FragmentTransaction FT = FM.beginTransaction();
- //Fragment newFrag02 = new FRAG02();
- FT.replace(R.id.container, new FRAG03());
- FT.addToBackStack(null);
- FT.commit();
- //mListener = (FRAG03.OnFragmentInteractionListener) MyActivity;
- //mListener.onFragmentInteraction(null);
- }
- }
- //===============================================================================
- FRAG02
- //===============================================================================
- package com.techlatin.vyoumans.blankfrag31;
- import android.app.Activity;
- import android.net.Uri;
- import android.os.Bundle;
- import android.app.Fragment;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- /**
- * A simple {@link Fragment} subclass.
- * Activities that contain this fragment must implement the
- * {@link FRAG02.OnFragmentInteractionListener} interface
- * to handle interaction events.
- * Use the {@link FRAG02#newInstance} factory method to
- * create an instance of this fragment.
- *
- */
- public class FRAG02 extends Fragment {
- // TODO: Rename parameter arguments, choose names that match
- // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
- private static final String ARG_PARAM1 = "param1";
- private static final String ARG_PARAM2 = "param2";
- // TODO: Rename and change types of parameters
- private String mParam1;
- private String mParam2;
- private OnFragmentInteractionListener mListener;
- /**
- * Use this factory method to create a new instance of
- * this fragment using the provided parameters.
- *
- * @param param1 Parameter 1.
- * @param param2 Parameter 2.
- * @return A new instance of fragment FRAG02.
- */
- // TODO: Rename and change types and number of parameters
- public static FRAG02 newInstance(String param1, String param2) {
- FRAG02 fragment = new FRAG02();
- Bundle args = new Bundle();
- args.putString(ARG_PARAM1, param1);
- args.putString(ARG_PARAM2, param2);
- fragment.setArguments(args);
- return fragment;
- }
- public FRAG02() {
- // Required empty public constructor
- }
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- if (getArguments() != null) {
- mParam1 = getArguments().getString(ARG_PARAM1);
- mParam2 = getArguments().getString(ARG_PARAM2);
- }
- }
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- // Inflate the layout for this fragment
- return inflater.inflate(R.layout.fragment_frag02, container, false);
- }
- // TODO: Rename method, update argument and hook method into UI event
- public void onButtonPressed(Uri uri) {
- if (mListener != null) {
- mListener.onFragmentInteraction(uri);
- }
- }
- @Override
- public void onAttach(Activity activity) {
- super.onAttach(activity);
- try {
- mListener = (OnFragmentInteractionListener) activity;
- } catch (ClassCastException e) {
- throw new ClassCastException(activity.toString()
- + " must implement OnFragmentInteractionListener");
- }
- }
- @Override
- public void onDetach() {
- super.onDetach();
- mListener = null;
- }
- /**
- * This interface must be implemented by activities that contain this
- * fragment to allow an interaction in this fragment to be communicated
- * to the activity and potentially other fragments contained in that
- * activity.
- * <p>
- * See the Android Training lesson <a href=
- * "http://developer.android.com/training/basics/fragments/communicating.html"
- * >Communicating with Other Fragments</a> for more information.
- */
- public interface OnFragmentInteractionListener {
- // TODO: Update argument type and name
- public void onFragmentInteraction(Uri uri);
- }
- }
- //===============================================================================
- FRAG03
- //===============================================================================
- package com.techlatin.vyoumans.blankfrag31;
- import android.app.Activity;
- import android.net.Uri;
- import android.os.Bundle;
- import android.app.Fragment;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- /**
- * A simple {@link Fragment} subclass.
- * Activities that contain this fragment must implement the
- * {@link FRAG03.OnFragmentInteractionListener} interface
- * to handle interaction events.
- * Use the {@link FRAG03#newInstance} factory method to
- * create an instance of this fragment.
- *
- */
- public class FRAG03 extends Fragment {
- // TODO: Rename parameter arguments, choose names that match
- // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
- private static final String ARG_PARAM1 = "param1";
- private static final String ARG_PARAM2 = "param2";
- // TODO: Rename and change types of parameters
- private String mParam1;
- private String mParam2;
- private OnFragmentInteractionListener mListener;
- /**
- * Use this factory method to create a new instance of
- * this fragment using the provided parameters.
- *
- * @param param1 Parameter 1.
- * @param param2 Parameter 2.
- * @return A new instance of fragment FRAG03.
- */
- // TODO: Rename and change types and number of parameters
- public static FRAG03 newInstance(String param1, String param2) {
- FRAG03 fragment = new FRAG03();
- Bundle args = new Bundle();
- args.putString(ARG_PARAM1, param1);
- args.putString(ARG_PARAM2, param2);
- fragment.setArguments(args);
- return fragment;
- }
- public FRAG03() {
- // Required empty public constructor
- }
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- if (getArguments() != null) {
- mParam1 = getArguments().getString(ARG_PARAM1);
- mParam2 = getArguments().getString(ARG_PARAM2);
- }
- }
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- // Inflate the layout for this fragment
- return inflater.inflate(R.layout.fragment_frag03, container, false);
- }
- // TODO: Rename method, update argument and hook method into UI event
- public void onButtonPressed(Uri uri) {
- if (mListener != null) {
- mListener.onFragmentInteraction(uri);
- }
- }
- @Override
- public void onAttach(Activity activity) {
- super.onAttach(activity);
- try {
- mListener = (OnFragmentInteractionListener) activity;
- } catch (ClassCastException e) {
- throw new ClassCastException(activity.toString()
- + " must implement OnFragmentInteractionListener");
- }
- }
- @Override
- public void onDetach() {
- super.onDetach();
- mListener = null;
- }
- /**
- * This interface must be implemented by activities that contain this
- * fragment to allow an interaction in this fragment to be communicated
- * to the activity and potentially other fragments contained in that
- * activity.
- * <p>
- * See the Android Training lesson <a href=
- * "http://developer.android.com/training/basics/fragments/communicating.html"
- * >Communicating with Other Fragments</a> for more information.
- */
- public interface OnFragmentInteractionListener {
- // TODO: Update argument type and name
- public void onFragmentInteraction(Uri uri);
- }
- }
- //===============================================================================
- activity_my.xml
- //===============================================================================
- <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:id="@+id/container"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".MyActivity"
- tools:ignore="MergeRootFrame" />
- //===============================================================================
- fragment_my.xml
- //===============================================================================
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- android:paddingBottom="@dimen/activity_vertical_margin"
- tools:context=".MyActivity$PlaceholderFragment">
- <TextView
- android:text="@string/txtFrag01"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/txtFrag0001" />
- <Button
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="Goto Frag 02"
- android:id="@+id/btnGotoFrag02"
- android:layout_centerVertical="true"
- android:layout_alignParentLeft="true"
- android:layout_alignParentStart="true"
- android:onClick="btnClickGotoFrag02" />
- </RelativeLayout>
- //===============================================================================
- fragment_FRAG02.xml
- //===============================================================================
- <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context="com.techlatin.vyoumans.blankfrag31.FRAG02">
- <!-- TODO: Update blank fragment layout -->
- <TextView
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:text="@string/txtFrag02"
- android:id="@+id/FRAG00002" />
- <Button
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="GOTO FRAG 03"
- android:id="@+id/btnGotoFRAG03"
- android:layout_gravity="left|center_vertical"
- android:onClick="btnClickGotoFrag03" />
- </FrameLayout>
- //===============================================================================
- fragment_FRAG03.xml
- //===============================================================================
- <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context="com.techlatin.vyoumans.blankfrag31.FRAG02">
- <!-- TODO: Update blank fragment layout -->
- <TextView
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:text="@string/txtFrag02"
- android:id="@+id/FRAG00002" />
- <Button
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="GOTO FRAG 03"
- android:id="@+id/btnGotoFRAG03"
- android:layout_gravity="left|center_vertical"
- android:onClick="btnClickGotoFrag03" />
- </FrameLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement