From cff319f0b250f365180fac9fffe8317dfbb297b7 Mon Sep 17 00:00:00 2001 From: Yurij Mantulin Date: Tue, 23 Sep 2014 15:19:17 +0300 Subject: [PATCH 1/3] Fixed callback if DiaglogDragment called from an other Fragment --- .../android/dirchooser/DirectoryChooserFragment.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/library/src/main/java/net/rdrei/android/dirchooser/DirectoryChooserFragment.java b/library/src/main/java/net/rdrei/android/dirchooser/DirectoryChooserFragment.java index 85a4aa4..87eb8f2 100644 --- a/library/src/main/java/net/rdrei/android/dirchooser/DirectoryChooserFragment.java +++ b/library/src/main/java/net/rdrei/android/dirchooser/DirectoryChooserFragment.java @@ -3,6 +3,7 @@ import android.app.Activity; import android.app.AlertDialog; import android.app.DialogFragment; +import android.app.Fragment; import android.content.DialogInterface; import android.content.res.Resources; import android.content.res.TypedArray; @@ -250,7 +251,14 @@ private void adjustResourceLightness() { public void onAttach(Activity activity) { super.onAttach(activity); try { - mListener = Option.some((OnFragmentInteractionListener) activity); + if (activity instanceof OnFragmentInteractionListener) { + mListener = Option.some((OnFragmentInteractionListener) activity); + } else { + Fragment owner = getTargetFragment(); + if (owner instanceof OnFragmentInteractionListener) { + mListener = Option.some((OnFragmentInteractionListener) owner); + } + } } catch (ClassCastException ignore) { } } From dbefae1899992d959d5b9991fac111769c3e6c73 Mon Sep 17 00:00:00 2001 From: Yurij Mantulin Date: Tue, 23 Sep 2014 15:52:13 +0300 Subject: [PATCH 2/3] Added Fragment sample to the Sample project. --- sample/src/main/AndroidManifest.xml | 10 +++- .../dirchooser/sample/DirChooserFragment.java | 56 +++++++++++++++++++ .../sample/DirChooserFragmentHolder.java | 29 ++++++++++ .../dirchooser/sample/DirChooserSample.java | 7 +++ .../activity_dir_chooser_fragment_holder.xml | 7 +++ sample/src/main/res/layout/fragment_blank.xml | 30 ++++++++++ .../fragment_dir_chooser_fragment_holder.xml | 12 ++++ sample/src/main/res/layout/main.xml | 7 +++ sample/src/main/res/values/dimens.xml | 5 ++ sample/src/main/res/values/strings.xml | 4 ++ 10 files changed, 164 insertions(+), 3 deletions(-) create mode 100644 sample/src/main/java/net/rdrei/android/dirchooser/sample/DirChooserFragment.java create mode 100644 sample/src/main/java/net/rdrei/android/dirchooser/sample/DirChooserFragmentHolder.java create mode 100644 sample/src/main/res/layout/activity_dir_chooser_fragment_holder.xml create mode 100644 sample/src/main/res/layout/fragment_blank.xml create mode 100644 sample/src/main/res/layout/fragment_dir_chooser_fragment_holder.xml create mode 100644 sample/src/main/res/values/dimens.xml diff --git a/sample/src/main/AndroidManifest.xml b/sample/src/main/AndroidManifest.xml index 3b06cb2..2bf8fc2 100644 --- a/sample/src/main/AndroidManifest.xml +++ b/sample/src/main/AndroidManifest.xml @@ -1,6 +1,6 @@ + package="net.rdrei.android.dirchooser.sample" > @@ -20,9 +20,13 @@ + android:parentActivityName="net.rdrei.android.dirchooser.sample.DirChooserSample" > + + diff --git a/sample/src/main/java/net/rdrei/android/dirchooser/sample/DirChooserFragment.java b/sample/src/main/java/net/rdrei/android/dirchooser/sample/DirChooserFragment.java new file mode 100644 index 0000000..8085bd2 --- /dev/null +++ b/sample/src/main/java/net/rdrei/android/dirchooser/sample/DirChooserFragment.java @@ -0,0 +1,56 @@ +package net.rdrei.android.dirchooser.sample; + + +import android.os.Bundle; +import android.app.Fragment; +import android.support.annotation.NonNull; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.Button; +import android.widget.TextView; + +import net.rdrei.android.dirchooser.DirectoryChooserFragment; + + +public class DirChooserFragment extends Fragment implements DirectoryChooserFragment.OnFragmentInteractionListener { + + private TextView mDirectoryTextView; + private DirectoryChooserFragment mDialog; + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + mDialog = DirectoryChooserFragment.newInstance("DialogSample", null); + mDialog.setTargetFragment(this, 0); + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + // Inflate the layout for this fragment + View view = inflater.inflate(R.layout.fragment_blank, container, false); + mDirectoryTextView = (TextView) view.findViewById(R.id.tvFragmentText); + + view.findViewById(R.id.btnStart).setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + mDialog.show(getFragmentManager(), null); + } + }); + + return view; + } + + + @Override + public void onSelectDirectory(@NonNull String path) { + mDirectoryTextView.setText(path); + mDialog.dismiss(); + } + + @Override + public void onCancelChooser() { + mDialog.dismiss(); + } +} diff --git a/sample/src/main/java/net/rdrei/android/dirchooser/sample/DirChooserFragmentHolder.java b/sample/src/main/java/net/rdrei/android/dirchooser/sample/DirChooserFragmentHolder.java new file mode 100644 index 0000000..3d856ab --- /dev/null +++ b/sample/src/main/java/net/rdrei/android/dirchooser/sample/DirChooserFragmentHolder.java @@ -0,0 +1,29 @@ +package net.rdrei.android.dirchooser.sample; + +import android.app.Activity; +import android.app.ActionBar; +import android.app.Fragment; +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; + + + +public class DirChooserFragmentHolder extends Activity { + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_dir_chooser_fragment_holder); + if (savedInstanceState == null) { + getFragmentManager().beginTransaction() + .add(R.id.container, new DirChooserFragment()) + .commit(); + } + } + +} diff --git a/sample/src/main/java/net/rdrei/android/dirchooser/sample/DirChooserSample.java b/sample/src/main/java/net/rdrei/android/dirchooser/sample/DirChooserSample.java index 37a56a9..b63e827 100644 --- a/sample/src/main/java/net/rdrei/android/dirchooser/sample/DirChooserSample.java +++ b/sample/src/main/java/net/rdrei/android/dirchooser/sample/DirChooserSample.java @@ -55,6 +55,13 @@ public void onClick(View v) { startActivity(fragmentSampleIntent); } }); + + findViewById(R.id.btnStartFragment).setOnClickListener(new OnClickListener() { + @Override + public void onClick(View view) { + startActivity(new Intent(DirChooserSample.this, DirChooserFragmentHolder.class)); + } + }); } @Override diff --git a/sample/src/main/res/layout/activity_dir_chooser_fragment_holder.xml b/sample/src/main/res/layout/activity_dir_chooser_fragment_holder.xml new file mode 100644 index 0000000..a7b343d --- /dev/null +++ b/sample/src/main/res/layout/activity_dir_chooser_fragment_holder.xml @@ -0,0 +1,7 @@ + diff --git a/sample/src/main/res/layout/fragment_blank.xml b/sample/src/main/res/layout/fragment_blank.xml new file mode 100644 index 0000000..f793021 --- /dev/null +++ b/sample/src/main/res/layout/fragment_blank.xml @@ -0,0 +1,30 @@ + + + + + + +