From 0320cb6627698c901ad02e4060b2cdd507e15151 Mon Sep 17 00:00:00 2001 From: Remypoc Date: Sat, 28 Jan 2017 19:51:28 +0100 Subject: [PATCH 01/31] Add a little example to show how fragments communicate with each other --- app/src/main/AndroidManifest.xml | 1 + .../AssociationDashboardActivity.java | 64 +++++++++++ .../ui/activites/AssociationMainFragment.java | 102 ++++++++++++++++++ .../activites/CreateAssociationFragment.java | 91 ++++++++++++++++ .../assogenda/ui/activites/MainActivity.java | 38 ++++++- .../layout/activity_association_dashboard.xml | 22 ++++ app/src/main/res/layout/activity_main.xml | 7 +- .../res/layout/fragment_association_main.xml | 26 +++++ .../layout/fragment_create_association.xml | 13 +++ app/src/main/res/values/strings.xml | 2 + 10 files changed, 363 insertions(+), 3 deletions(-) create mode 100644 app/src/main/java/fr/paris10/projet/assogenda/assogenda/ui/activites/AssociationDashboardActivity.java create mode 100644 app/src/main/java/fr/paris10/projet/assogenda/assogenda/ui/activites/AssociationMainFragment.java create mode 100644 app/src/main/java/fr/paris10/projet/assogenda/assogenda/ui/activites/CreateAssociationFragment.java create mode 100644 app/src/main/res/layout/activity_association_dashboard.xml create mode 100644 app/src/main/res/layout/fragment_association_main.xml create mode 100644 app/src/main/res/layout/fragment_create_association.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index ee62589..3815c63 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -15,6 +15,7 @@ + \ No newline at end of file diff --git a/app/src/main/java/fr/paris10/projet/assogenda/assogenda/ui/activites/AssociationDashboardActivity.java b/app/src/main/java/fr/paris10/projet/assogenda/assogenda/ui/activites/AssociationDashboardActivity.java new file mode 100644 index 0000000..738f218 --- /dev/null +++ b/app/src/main/java/fr/paris10/projet/assogenda/assogenda/ui/activites/AssociationDashboardActivity.java @@ -0,0 +1,64 @@ +package fr.paris10.projet.assogenda.assogenda.ui.activites; + +import android.app.Fragment; +import android.os.Bundle; +import android.app.FragmentTransaction; +import android.support.v7.app.AppCompatActivity; +import android.util.Log; + +import fr.paris10.projet.assogenda.assogenda.R; + +public class AssociationDashboardActivity extends AppCompatActivity implements + AssociationMainFragment.OnFragmentInteractionListener, + CreateAssociationFragment.OnFragmentInteractionListener { + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_association_dashboard); + Log.i(this.getClass().getCanonicalName(), "Entre dans onCreate"); + + Fragment associationMainFragment = new AssociationMainFragment(); + FragmentTransaction transaction = getFragmentManager().beginTransaction(); + transaction + .replace(R.id.activity_association_dashboard_fragment_container, + associationMainFragment) + .commit(); + } + + @Override + protected void onStart() { + super.onStart(); + Log.i(this.getClass().getCanonicalName(), "Entre dans onStart"); + } + + @Override + protected void onResume() { + super.onResume(); + Log.i(this.getClass().getCanonicalName(), "Entre dans onResume"); + } + + @Override + protected void onDestroy() { + super.onDestroy(); + Log.i(this.getClass().getCanonicalName(), "Entre dans onDestroy"); + } + + @Override + public void onAssociationDashboardFragmentInteraction() { + Log.i(this.getClass().getCanonicalName(), + "Entre dans onAssociationDashboardFragmentInteraction"); + + Fragment createAssociationFragment = new CreateAssociationFragment(); + FragmentTransaction transaction = getFragmentManager().beginTransaction(); + transaction + .replace(R.id.activity_association_dashboard_fragment_container, + createAssociationFragment) + .commit(); + } + + @Override + public void onCreateAssociationFragmentInteraction() { + + } +} diff --git a/app/src/main/java/fr/paris10/projet/assogenda/assogenda/ui/activites/AssociationMainFragment.java b/app/src/main/java/fr/paris10/projet/assogenda/assogenda/ui/activites/AssociationMainFragment.java new file mode 100644 index 0000000..36a09cc --- /dev/null +++ b/app/src/main/java/fr/paris10/projet/assogenda/assogenda/ui/activites/AssociationMainFragment.java @@ -0,0 +1,102 @@ +package fr.paris10.projet.assogenda.assogenda.ui.activites; + +import android.app.Activity; +import android.content.Context; +import android.os.Bundle; +import android.app.Fragment; +import android.util.Log; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.Button; + +import fr.paris10.projet.assogenda.assogenda.R; + +/** + * A simple {@link Fragment} subclass. + * Create an instance of this fragment. + */ +public class AssociationMainFragment extends Fragment implements View.OnClickListener { + + private OnFragmentInteractionListener mListener; + + public AssociationMainFragment() { + } + + /** + * Use this factory method to create a new instance of + * this fragment using the provided parameters. + * + * @return A new instance of fragment AssociationMainFragment. + */ + public static AssociationMainFragment newInstance() { + AssociationMainFragment fragment = new AssociationMainFragment(); + return fragment; + } + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + View v = inflater.inflate(R.layout.fragment_association_main, container, false); + Button createAssociationButton = + (Button) v.findViewById(R.id.fragment_association_main_button_create_association); + createAssociationButton.setOnClickListener(this); + return v; + } + + //For API Level 23 + @Override + public void onAttach(Context context) { + super.onAttach(context); + Log.i(this.getClass().getCanonicalName(), "Entre dans onAttach Context"); + if (context instanceof CreateAssociationFragment.OnFragmentInteractionListener) { + mListener = (AssociationMainFragment.OnFragmentInteractionListener) context; + } else { + throw new RuntimeException(context.toString() + + " must implement OnFragmentInteractionListener"); + } + } + + //For API Level 22 (deprecated) + @Override + public void onAttach(Activity activity) { + super.onAttach(activity); + Log.i(this.getClass().getCanonicalName(), "Entre dans onAttach Activity"); + try { + mListener = (AssociationMainFragment.OnFragmentInteractionListener) activity; + } catch (ClassCastException e) { + throw new ClassCastException(activity.toString() + + " must implement OnHeadlineSelectedListener"); + } + } + + + @Override + public void onDetach() { + super.onDetach(); + Log.i(this.getClass().getCanonicalName(), "Entre dans onDetach"); + mListener = null; + } + + @Override + public void onClick(View view) { + Log.i(this.getClass().getCanonicalName(), "Entre dans onClick "); + mListener.onAssociationDashboardFragmentInteraction(); + } + + /** + * 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. + */ + public interface OnFragmentInteractionListener { + + void onAssociationDashboardFragmentInteraction(); + } +} \ No newline at end of file diff --git a/app/src/main/java/fr/paris10/projet/assogenda/assogenda/ui/activites/CreateAssociationFragment.java b/app/src/main/java/fr/paris10/projet/assogenda/assogenda/ui/activites/CreateAssociationFragment.java new file mode 100644 index 0000000..56859b6 --- /dev/null +++ b/app/src/main/java/fr/paris10/projet/assogenda/assogenda/ui/activites/CreateAssociationFragment.java @@ -0,0 +1,91 @@ +package fr.paris10.projet.assogenda.assogenda.ui.activites; + +import android.app.Activity; +import android.content.Context; +import android.os.Bundle; +import android.app.Fragment; +import android.util.Log; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; + +import fr.paris10.projet.assogenda.assogenda.R; + +/** + * A simple {@link Fragment} subclass. + * Create an instance of this fragment. + */ +public class CreateAssociationFragment extends Fragment { + + private OnFragmentInteractionListener mListener; + + public CreateAssociationFragment() { + } + + /** + * Use this factory method to create a new instance of + * this fragment using the provided parameters. + * + * @return A new instance of fragment AssociationMainFragment. + */ + public static CreateAssociationFragment newInstance() { + CreateAssociationFragment fragment = new CreateAssociationFragment(); + return fragment; + } + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + View v = inflater.inflate(R.layout.fragment_create_association, container, false); + return v; + } + + //For API Level 23 + @Override + public void onAttach(Context context) { + super.onAttach(context); + Log.i(this.getClass().getCanonicalName(), "Entre dans onAttach Context"); + if (context instanceof OnFragmentInteractionListener) { + mListener = (CreateAssociationFragment.OnFragmentInteractionListener) context; + } else { + throw new RuntimeException(context.toString() + + " must implement OnFragmentInteractionListener"); + } + } + + //For API Level 22 (deprecated) + @Override + public void onAttach(Activity activity) { + super.onAttach(activity); + Log.i(this.getClass().getCanonicalName(), "Entre dans onAttach Activity"); + try { + mListener = (CreateAssociationFragment.OnFragmentInteractionListener) activity; + } catch (ClassCastException e) { + throw new ClassCastException(activity.toString() + + " must implement OnHeadlineSelectedListener"); + } + } + + @Override + public void onDetach() { + super.onDetach(); + Log.i(this.getClass().getCanonicalName(), "Entre dans 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. + */ + public interface OnFragmentInteractionListener { + + void onCreateAssociationFragmentInteraction(); + } +} \ No newline at end of file diff --git a/app/src/main/java/fr/paris10/projet/assogenda/assogenda/ui/activites/MainActivity.java b/app/src/main/java/fr/paris10/projet/assogenda/assogenda/ui/activites/MainActivity.java index ab42f3c..2ecb48e 100644 --- a/app/src/main/java/fr/paris10/projet/assogenda/assogenda/ui/activites/MainActivity.java +++ b/app/src/main/java/fr/paris10/projet/assogenda/assogenda/ui/activites/MainActivity.java @@ -1,7 +1,11 @@ package fr.paris10.projet.assogenda.assogenda.ui.activites; +import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; +import android.util.Log; +import android.view.View; +import android.widget.Button; import fr.paris10.projet.assogenda.assogenda.R; @@ -11,5 +15,37 @@ public class MainActivity extends AppCompatActivity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); + Log.i(this.getClass().getCanonicalName(), "Entre dans onCreate"); + + Button associationButton = (Button) findViewById(R.id.activity_main_button_association); + associationButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + startAssociationActivity(); + } + }); + } + + public void startAssociationActivity() { + Intent intent = new Intent(this, AssociationDashboardActivity.class); + startActivity(intent); + } + + @Override + protected void onStart() { + super.onStart(); + Log.i(this.getClass().getCanonicalName(), "Entre dans onStart"); + } + + @Override + protected void onResume() { + super.onResume(); + Log.i(this.getClass().getCanonicalName(), "Entre dans onResume"); + } + + @Override + protected void onDestroy() { + super.onDestroy(); + Log.i(this.getClass().getCanonicalName(), "Entre dans onDestroy"); } -} +} \ No newline at end of file diff --git a/app/src/main/res/layout/activity_association_dashboard.xml b/app/src/main/res/layout/activity_association_dashboard.xml new file mode 100644 index 0000000..3dd1a8e --- /dev/null +++ b/app/src/main/res/layout/activity_association_dashboard.xml @@ -0,0 +1,22 @@ + + + + + + + + diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index df1cacf..cf6c88c 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -10,8 +10,11 @@ android:paddingTop="@dimen/activity_vertical_margin" tools:context="fr.paris10.projet.assogenda.assogenda.ui.activites.MainActivity"> - + android:layout_alignParentTop="true" + android:layout_centerHorizontal="true" + android:id="@+id/activity_main_button_association" /> diff --git a/app/src/main/res/layout/fragment_association_main.xml b/app/src/main/res/layout/fragment_association_main.xml new file mode 100644 index 0000000..10e8589 --- /dev/null +++ b/app/src/main/res/layout/fragment_association_main.xml @@ -0,0 +1,26 @@ + + + + + + +