Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed callback if DialogFragment called from an other Fragment #39

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we now have proper checks in place, we no longer need to catch the ClassCastException

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

^

mListener = Option.some((OnFragmentInteractionListener) activity);
} else {
Fragment owner = getTargetFragment();
if (owner instanceof OnFragmentInteractionListener) {
mListener = Option.some((OnFragmentInteractionListener) owner);
}
}
} catch (ClassCastException ignore) {
}
}
Expand Down
10 changes: 7 additions & 3 deletions sample/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.rdrei.android.dirchooser.sample">
package="net.rdrei.android.dirchooser.sample" >

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Expand All @@ -20,9 +20,13 @@
</activity>
<activity
android:name="net.rdrei.android.dirchooser.sample.DirChooserFragmentSample"
android:configChanges="orientation|screenSize"
android:label="@string/app_name"
android:parentActivityName="net.rdrei.android.dirchooser.sample.DirChooserSample"
android:configChanges="orientation|screenSize" >
android:parentActivityName="net.rdrei.android.dirchooser.sample.DirChooserSample" >
</activity>
<activity
android:name="net.rdrei.android.dirchooser.sample.DirChooserFragmentHolder"
android:label="@string/title_activity_dir_chooser_fragment_holder" >
</activity>
</application>

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you reduce this to the minimum changes necessary?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can make a new push only with changes in the library project. Or the other solution could be to combine all the changes in a one Activity. Because at the moment there is a necessary method call for a Fragment ( mDialog.setTargetFragment(this, REQUEST_CODE); ), so I think the sample will be useful. And what do you think ?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a tough one. I think I'd prefer the way you took here and keep them separate, because I think that keeping the example simple is very important for its usefulness.

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
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 static final int REQUEST_CODE = 0;

private TextView mDirectoryTextView;
private DirectoryChooserFragment mDialog;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mDialog = DirectoryChooserFragment.newInstance("DialogSample", null);

// You must set this Fragment to the Dialog for receiving the callback.
mDialog.setTargetFragment(this, REQUEST_CODE);
}

@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();
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<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="net.rdrei.android.dirchooser.sample.DirChooserFragmentHolder"
tools:ignore="MergeRootFrame" />
30 changes: 30 additions & 0 deletions sample/src/main/res/layout/fragment_blank.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<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"
android:padding="8dp"
tools:context="net.rdrei.android.dirchooser.sample.DirChooserFragment">

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/directory_selected_label"
android:textAppearance="?android:attr/textAppearanceMedium"/>

<TextView
android:id="@+id/tvFragmentText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="monospace"
android:text="@string/none"
tools:ignore="UnusedAttribute"
android:layout_marginTop="24dp" />

<Button
android:id="@+id/btnStart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/title_start_button" />

</FrameLayout>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/root"
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="net.rdrei.android.dirchooser.sample.DirChooserFragmentHolder$PlaceholderFragment">

</FrameLayout>
7 changes: 7 additions & 0 deletions sample/src/main/res/layout/main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,11 @@
android:layout_marginTop="48dp"
android:text="@string/btn_switch"/>

<Button
android:id="@+id/btnStartFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="48dp"
android:text="@string/btn_start"/>

</LinearLayout>
5 changes: 5 additions & 0 deletions sample/src/main/res/values/dimens.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
</resources>
4 changes: 4 additions & 0 deletions sample/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,9 @@
<string name="none">none</string>
<string name="btn_choose">Choose in Dialog</string>
<string name="btn_switch">Switch to Dialog Sample</string>
<string name="title_start_button">Start picker</string>
<string name="title_activity_dir_chooser_fragment_holder">DirChooserFragmentHolder</string>
<string name="btn_start">Start Activity with Fragment</string>


</resources>