intercept DialogFragment dismiss inside activity - android

I have an activity
public class ShowFileActivity extends FragmentActivity
and when occours some event, this class call a DialogFragment
public class ConfirmDialog extends DialogFragment
that is a simple confirm dialog (with "dismiss" and "ok" button).
If user press dismiss button, i call
dismiss()
and come back to ShowFileActivity.
Else, if user press ok, after made some operations, after call dismiss on dialog, i would go back to parent activity of ShowFileActivity.
There's a way to do it? Does DialogFragment launch any event to his parent view?

What you can do is to call a method of the containing activity from inside the fragment.
As per any other fragment, you can call getActivity() which returns the containing activity.
#Override
public void onDismiss(DialogInterface dialog) {
ShowFileActivity parent = (ShowFileActivity) getActivity();
parent.doWhateverYouWantWhenDialogDismissed();
}
Another (more fancy) approach would be to use an event bus such as otto or greenrobot.

Related

Detect Bottom sheet dialog fragment dismiss on base fragment

I have fragment which contains recycleview with list. in fragment i'm calling another bottom sheet dialog fragment, i Want to know when i dismiss that bottom sheet dialog fragment how to refresh base fragments list.
I Have Tried onpause and onresume method in base fragment. Please help me to solve the issue.
This method trigger when dialog fragment dismissed. To override the method in dialog fragment.
#Override
public void onDismiss(#NonNull DialogInterface dialog) {
super.onDismiss(dialog);
// use interface to callback method in base fragment
}
I'm not sure, but I tried blow code and it worked.
#Override
public void dismiss() {
super.dismiss();
((MyActivity)context).myRefreshFunction();//
}

Android custom Dialog not showing after FragmentActivity recreate

On my app i have a ViewPager inside FragmentActivity that contain a fragments. In one of that fragment (called FragOne) i have a button that on tap open a custom Dialog (a Class extends android.app.Dialog) on certain position of screen:
myButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
MyCustomDialog = new MyCustomDialog(getContext());
dialog.getWindow().setFlags(
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
dialog.getWindow().getDecorView().setSystemUiVisibility(
getActivity().getWindow().getDecorView().getSystemUiVisibility());
dialog.show();
dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
dialog.getWindow().setLayout(myWidth, myHeight);
}
});
When user perform some action on dialog some tasks are executed, recreate() of FragmentActivity is called, then dialog dismiss itself.
Now happen that if try to open again the dialog by press the button, i can't see it anymore. For make dialog appear i need to go to change viewpager current visible fragment, then go back to FragOne.
How can i do for let open dialog again without change fragment and go back to FragOne?

Looking for best way to implement modal dialog in onPause

I have fragment with EditTexts that user can change.
As well I have back button on Toolbar (and physical back button too)
When the user hits back, I check if the data was changed and if it was - I need to open dialog and ask the user "Do you want to save changes?". Get the click and act accordingly (positive or negative answer).
The best place to save the data (maybe I wrong) is in onPause of this fragment.
The problem is with the dialog - it is not modal and while it's showing the question and waits for user reaction - the fragment under it disappears and previous come back from stack.
I need to "pause the onPause" with the dialog until the user make his choice. What the easy (or most correct) way to do it?
#Override
public void onPause() {
if (!(text.getText().toString().equals(user.getName())))
{
new MaterialDialog.Builder(getActivity())
.title("Save changes?")
.content("You changed you personal details, save changes?")
.cancelable(false)
.positiveText("Save")
.negativeText("Discard")
.onPositive(new MaterialDialog.SingleButtonCallback() {
#Override
public void onClick(#NonNull MaterialDialog dialog, #NonNull DialogAction which) {
save();
}})
.show();
}
super.onPause();
}
If I want to do it before onPause - I'll need to catch the Toolbar's back button and physical back button - seems too much work for this. Looking for elegant way.
Thank you
onPause is called when the activity moves into the background (not being shown on the screen anymore).
To show a dialog before the app goes off screen, you can override the "onBackPressed" function in an activity.
#Override
public void onBackPressed() {
performBackPressed();
}
public void performBackPressed(){
//show your dialog here
//call finish(); when done to close app
}
You can call the performBackPressed() method whenever the Toolbar's back button is pressed too.
If you're trying to show a popup from a fragment when back is pressed, then you still have to override that method in the activity, then notifiy the fragment whenever back is pressed.
In my app I have a container in the activity that holds all my fragments. I use this code to get the currently active fragment:
YourFragmentClass curFrag = (YourFragmentClass) fragmentManager.findFragmentById(R.id.fragment_container);
then I have a method in the fragment called onBackPressed() and I just call that on the fragment I just got.

DialogFragment dismiss() does not pop backstack

I have a simple DialogFragment that calls dismiss when exits, according to the documentation:
public void dismiss()
Dismiss the fragment and its dialog. If the fragment was added to the
back stack, all back stack state up to and including this entry will
be popped. Otherwise, a new transaction will be committed to remove
the fragment.
however, I found that the fragment is still on the backstack after calling dismiss() so I have to click back button to clear it. Does anyone know why ?
here's my code:
public void onCreate(Bundle b) {
super.onCreate(b);
setContentView(R.layout.test_layout);
class MyDialogFragment extends DialogFragment implements OnClickListener{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.hello_world, container, false);
Button b = (Button)v.findViewById(R.id.btn);
b.setOnClickListener(this);
return v;
}
#Override
public void onClick(View v) {
dismiss();
}
}
getFragmentManager().beginTransaction().add(android.R.id.content, new MyDialogFragment(), "test").addToBackStack("b").commit();
}
#Override
public void onBackPressed() {
if (getFragmentManager().getBackStackEntryCount() > 0 ){
getFragmentManager().popBackStack();
} else {
super.onBackPressed();
}
}
}
I also found out that if I don't override onBackPressed(), the back button simple doesn't work, no matter how many fragments I add to the activity, the back button always exits the activity right away.
I can confirm what #Luksprog said in his comment: the dialog must be started through show(FragmentTransaction, String).
Note after looking the source: make sure to call addToBackStack(String) on the supplied transaction or else it still won't work.
That it's a wrong way to create a DialogFragment.
Never ever use the FragmentManager to show a DialogFragment. To be shown there are a method called show(FragmentTransacion, String).
In java:
MyDialogFragment mDialogFragment = new MyDialogFragment();
mDialogFragment.show(getFragmentManager(), "MyDialogFragment");
For another hand, to dismiss the dialog just do this:
mDialogFragment.dismiss()
Another think that I would like to highlight is that the MyDialogFragment class is defined inner onCreate method :'(
Please, define the class outside the method or in another file if you want :)
Good Look!
dismiss()
findNavController().navigate(FirstBottomSheetDialogDirections.actionFirstSheetToSecondSheet())
This code is always the wrong thing to do: dismiss() is an asynchronous operation that doesn't actually dismiss anything immediately. That is unlike the navigate() which does immediately update the NavController's state, stacking the new dialog destination on top of the previous one.
This means that when the asynchronous dismiss actually happens, it correctly removes the dialog and, because it is a navigation stack, removes everything on top of it - including your second dialog. However, due to a bug in the DialogFragmentNavigator, we don't actually dismiss that second dialog, which is why it appears to work, despite everything actually already being internally out of sync (thus causing the later crash).
The correct way to pop a destination and navigate to a new destination as an atomic, immediate operation is to use popUpTo and popUpToInclusive. Therefore you can fix the sample app by removing the call to dismiss() and updating the action to pop the first dialog as part of the navigate call:
<action
android:id="#+id/action_firstSheet_to_secondSheet"
app:destination="#id/secondSheet"
app:popUpTo="#id/firstSheet"
app:popUpToInclusive="true"/>
This correctly pops the first dialog off the back stack and then navigates to the new dialog destination.
please refer this link : https://issuetracker.google.com/issues/191073055

How do I call a method of a running child activity from a parent activity?

I am developing an android app which has 3 tabs, created in MainActivity.java. Every tab has its own activity. In those activities I have a method called "Refresh()" to update the listview in that tab.
When the user clicks on a button the method "refreshTab(View v)" is called.
// Tab refreshen
public void refreshTab (View v) {
Activity MyActivity = this.getCurrentActivity();
MyActivity.Refresh();
}
This is throwing "The Method Refresh() is undefined for the type Activity. However, "MyActivity" is filled with the tab activity.
How would I go about getting this to work?
You need to cast the activity to your type of activity. Right now you are trying to call the Android class activity, which does not have a "Refresh" function.
Your button handler is a little over-complicated (even though it's only two lines)...
Just do something like:
// Tab refreshen
public void refreshTab (View v) {
Refresh();
}
If the way you've defined your OnClickListener is directly inline (but still within your activity's class), you may need to add a little direction, where MyClassType is the name of your class that extends Activity:
// Tab refreshen
public void refreshTab (View v) {
MyClassType.this.Refresh();
}

Categories

Resources