Show Dialog, on Fragment working - android

I have a Fragment and at its onStart(), I made a lot of BD querys, that are long for 15 seconds. I want to show a Progress Dialog, with a "loading circle".
My app, has just one Activity, the Main. This, starts the first Fragment, and from this, I jump to other Fragment, and the same for the next. For to pass from one Fragment to other, I use the next static method.
public static startFragment(FragmentTransaction FT, Fragment newFragmentToOpen)
{
FT.replace(R.id.Container, newFragmentToOpen);
FT.addToBackStack(null);
FT.commit();
}
I tried make a Thread in that method what starts the Progress Dialog, but it didn´t work. I tried to start the dialog on the OnCreate() method of the Fragment, and that didn´t work too.
What can I do?
Thank you.
(Edit)
I tried to start the Progress Dialog inside of the Fragment, at the onCreateView() and at onViewCreated(). It didn´t work, but if I debug step by step, I see that at the moment when the flow goes to the onStart() (where it is for 10 seconds usinig a DB), the ProgressDialog stops.
By this test, I understand, that the ProgressDialog is correctly showed, but when I go to onStart(), it disappear.
(Edit)
The problem is, that at the onStart() of the Fragment, I am working on the views, addking elements. I need, that while these elements are loading, a dialog be showed, because now, for the user it seems that the app is sttoped while it loads. I would need something like this:
public static startFragment(FragmentTransaction FT, Fragment newFragmentToOpen)
{
startProgressDialog();
FT.replace(R.id.container, newFragmentToOpen);
FT.addToBackStack(null);
FT.commit();
stopProgressDialog();
}
But it doesn´t work, because the progress Dialog just begins when the view of the Fragment is fully loaded.

You should try showing the dialog in onCreateView() since this is where the UI components are displayed in a Fragment

I found the solution.
To create STATIC vars.These will contain the info that I am going to use in the OnStart() of the Fragment.
Before to make the transition of the Fragment, start the Progress Dialog.
After of starting the Progress Dialog, start a THREAD.
(INTO THE THREAD) Load the info into the static vars.
(INTO THE THREAD) After load all the info, close the Progress Dialog.
(INTO THE THREAD) Call to the Fragment to load.
Finish of the Thread.
Into de Fragment, at OnStart(), take the info from the STATIC vars.

Related

Calling Fragment not paused when showing DialogFragment

In one part of my application, I show the user a ListView. When the user presses an item in the list, a DialogFragment is shown.
#Override
public void onClick() {
android.support.v4.app.FragmentTransaction ft = getFragment().getFragmentManager().beginTransaction();
ft.addToBackStack(null);
SingleSettingDialogFragment dialog = SingleSettingDialogFragment.newInstance(...);
dialog.show(ft, "Single");
}
The DialogFragment have the following structure:
#Override
public Dialog onCreateDialog(final Bundle savedInstanceState) {
super.onCreateDialog(savedInstanceSate);
AlertDialog dialog = new AlertDialog.Builder(...)
...
.create();
...
return dialog;
}
When the user exits the DialogFragment, I expect the onResume() method of the calling fragment to be called, but it is not.
So what's so special about DialogFragments? Why aren't the calling Fragment paused when the Dialog is shown? And how can it be achieved?
I haven't found any reference to this behaviour in the Docs, so references is a plus.
This may help you: link
In fact a FragmentDialog is not an activity on itself but it is part of the same activity which contains the calling fragment.
It means that the fragment is not paused when dialogFragment is shown.
Citing the source I gave you, a fragment is paused when: Another activity is in the foreground and has focus, but the activity in which this fragment lives is still visible (the foreground activity is partially transparent or doesn't cover the entire screen).
Hope that helps
One way to deal with this is to embed your DialogFragment within an Activity and display the activity as a Dialog, there's a tip in the following link that explains how:
http://developer.android.com/guide/topics/ui/dialogs.html
You can use this to update the underlying Fragment because when the Dialog (which is an Activity) is finished, onResume() will be called on the underlying fragment. Add code to update the state of the fragment in the onResume() method and that's all there is too it.
Other approach is that you can override OnDismiss() method and use call back listener in it which will call back the parent fragment.
These are the suggestions from my side, Hope it will get any kind of clue.

DialogFragment and addToBackstack method connection

The DialogFragment's combination with BackStack confuses me and maybe someone could help me out.
From what I found out, there are 2 common ways of displaying the DialogFragment. Either through show() method or by normal adding the fragment through transaction (from the checkup that's essentialy what show() does internaly).
The problem I have is with understanding of where addToBackstack() method comes into this whole process, especialy when you add transaction to backstack prior to calling on show() method, like in this sample:
// DialogFragment.show() will take care of adding the fragment
// in a transaction. We also want to remove any currently showing
// dialog, so make our own transaction and take care of that here.
FragmentTransaction ft = getFragmentManager().beginTransaction();
Fragment prev = getFragmentManager().findFragmentByTag("dialog");
if (prev != null) {
ft.remove(prev);
}
ft.addToBackStack(null);
// Create and show the dialog.
DialogFragment newFragment = MyDialogFragment.newInstance(mStackLevel);
newFragment.show(ft, "dialog");
In the sample above, before displaying the new DialogFragment, we check for other DialogFragment that could be displayed, we remove it and add this procedure to the backstack (I assume that this is for the purpose of displaying the previous DialogFragment, when the new DialogFragment is removed). Afterwards we display the new DialogFragment through show() method. But I fail to see any difference between this approach, and just calling show() method. I just checked on a Test project with displaying multiple DialogFragments one on top of eachother in a succesion, and the internal implementation of DialogFragment handles everything automaticaly, meaning that when I touch back button, the previous DialogFragment is displayed nevertheless.
So, why the addToBackstack is being used in context of DialogFragments?
Alright, after writing more code using the DialogFragment solution including more tests I came to the reason (most likely the key reason) of why to use the addToBackStack way, right after removing the previous dialog.
The reason for it (silly me that I missed that) is that it will make sure only one dialog is visible at one point in time. The show() method does exactly what it says, it "shows" a new dialog fragment, but does absolutely nothing with any previous visible dialog, so in essence all the dialogs using show() method will be stacked on top of the previous dialog. My error was that I didn't realize that until I made dialogs different in size. If all dialogs are of the same size, then the most top one will be hiding all the other dialogs.
So to summarize, show() method does not hide/remove any dialog that is already present on the screen. If we want to do that, we need to do the transaction manually, which of course must include the removing of the previous dialog as well as adding this transaction to the back stack so that when user presses the back button, the previous dialog will reemerge.

Dialog Fragment onactivity result

my question is for Dialog Fragment being cshown from a activity, then on the dialog fragment we initiate the onactivityresult(..) the dialog is waiting for result from activity.
when activity top one returns, the dialog fragment is not visible.
the fragment is actually there, under the parent activity who originally showed the fragment.
wired case
in case the orientation were changed when fragment was displayed the flow works flawlessly.
this has only started to happen 4.2 on Nexus 7, any body can help so that the fragment still keeps visible and get the result back
Have you looked at the google documentation on dialog fragments?
http://developer.android.com/guide/topics/ui/dialogs.html
I think the easiest way would be to create an interface for your dialog fragment,
then your caller activity would implement that interface.
Then you can show your dialog fragment by doing something like:
MyDialogFragment dialog = new MyDialogFragment();
dialog.show(getSupportFragmentManager(), "fragment_new");
The documentation has plenty of examples.

Proper way of dismissing DialogFragment while application is in background

I started using DialogFragment, because they are working nicely through orientation changes, and stuff. But there is nasty problem I encountered.
I have AsyncTask that shows progress DialogFragment and dismisses it onPostExecute. Everything works fine, except when onPostExecute happens while application is in background (after pressing Home button, for example). Then I got this error on DialogFragment dismissing - "Can not perform this action after onSaveInstanceState". Doh. Regular dialogs works just fine. But not FragmentDialog.
So I wonder, what is the proper way of dismissing DialogFragment while application is in background? I haven't really worked with Fragments a lot, so I think that I'm just missing something.
DialogFragment has a method called dismissAllowingStateLoss()
This is what I did (df == dialogFragment):
Make sure that you call the dialog this way:
df.show(getFragmentManager(), "DialogFragment_FLAG");
When you want to dismis the dialog make this check:
if (df.isResumed()){
df.dismiss();
}
return;
Make sure that you have the following in the onResume() method of your fragment (not df)
#Override
public void onResume(){
Fragment f = getFragmentManager().findFragmentByTag("DialogFragment_FLAG");
if (f != null) {
DialogFragment df = (DialogFragment) f;
df.dismiss();
}
super.onResume();
}
This way, the dialog will be dismissed if it's visible.. if not visible the dialog is going to be dismisded next the fragment becomes visible (onResume)...
This is what I had to do to achieve what you want:
I have a Fragment activity on which i was showing a dialog fragment named fragment_RedemptionPayment which is globally declared at the top. The following code dismisses the DialogFragment if it was showing before the activity goes in background and comes back in foreground.
#Override
public void onResume() {
super.onResume();
if(fragment_RedemptionPayment.isVisible()){
fragment_RedemptionPayment.dismiss();
}
}
Another new way of checking the state before calling dismiss is this:
if(!dialog.isStateSaved){
dialog.dismiss()
} else {
//Change the UI to suit your functionality
}
In this way its is checked that state is saved or not, basically on pause and onSaveInstanceState has been called.
For Java you can use isStateSaved()
A solution that might work is setting Fragment.setRetainInstance(true) in your dialogfragment, but that's not the prettiest of fixes.
Sometimes I have noticed that I have to queue up my dialog actions to let the framework restore the state first. If you can get hold of the current Looper (Activity.getMainLooper()) and wrap that in a Handler you could try passing your dismissal to the back of the queue by posting a runnable on that queue.
I often end up using a separate fragment that it retaininstance(true) that has a ResultReceiver. So i pass on that result receiver to my jobs and handle callbacks in its onReceive (often as a router for other receivers). But that might be a bit more work than it is worth if you are using async tasks.

Dismiss dialog after screen orientation change

1) I launch a background task (via AsyncTask)
new FindJourneyTask().execute(); // FindJourneyTask extends AsyncTask
2) Still in the main thread (just before new thread is launched) I create a dialog with showDialog(dialogId)
// this method is in FindJourneyTask
protected void onPreExecute() {
showDialog(DIALOG_FINDING_JOURNEY);
}
3) Screen orientation changes and the Activity is recreated
4) How can I now dismiss the dialog from the FindJourneyTask? Calling dismissDialog(dialogId) does nothing.
// this method is in FindJourneyTask
protected void onPostExecute(FindJourneyResult result) {
dismissDialog(DIALOG_FINDING_JOURNEY); // does nothing
}
This is a common problem, and there are no real good solutions. The issue is that on screen orientation change, the entire Activity is destroyed and recreated. At the same time, the Dialog you previously had is re-created in the new Activity, but the old background task still refers to the old Activity when it tries to dismiss the dialog. The result is that it dismisses a dialog which was long ago destroyed, rather than dismissing the dialog the new orientation created.
There are three basic solutions:
Override the default orientation-handling code so that your Activity is not destroyed upon rotation. This is probably the least satisfactory answer, as it blocks a lot of code that is automatically run upon orientation changes.
Create a static member variable of your Activity that references the Activity itself, so you can call STATIC_ACTIVITY_VARIABLE.dismissDialog().
Code a solution in which the background task keeps track of the current Activity and updates itself as necessary.
These three solutions are discussed at length here: http://groups.google.com/group/android-developers/browse_thread/thread/bf046b95cf38832d/
There is a better solution to this problem now which involves using fragments.
If you create a dialog using DialogFragment, then this fragment will be responsible for maintaining your dialog's lifecycle. When you show a dialog, you supply a tag for your fragment (DialogFragment.show()). When you need to access your dialog, you just look for the necessary DialogFragment using FragmentManager.findFragmentByTag instead of having a reference to the dialog itself.
This way if device changes orientation, you will get a new fragment instead of the old one, and everything will work.
Here's some code based also in #peresisUser answer:
public void onSaveInstanceState(Bundle outState) {
AppCompatActivity activity = (AppCompatActivity) context;
FragmentManager fragmentManager = activity.getSupportFragmentManager();
DialogFragment dialogFragment = (DialogFragment) fragmentManager.findFragmentByTag("your_dialog_tag");
if(dialogFragment!=null) {
Dialog dialog = dialogFragment.getDialog();
if(dialog!=null && dialog.isShowing()) {
dialogFragment.dismiss();
}
}
}
This is long after the question was asked and answered, but i stumbled upon this problem also and wanted to share my solution...
I check in onSavedInstance() which runs on orientation change, whether the dialog is showing or not with dialog.isShowing(), and pass it into outState variable. Then in your onCreate(), you check this var if it's true. If it is, you simply dismiss your dialog with dialog.dismiss()
Hope this helps others :()
I tried adding setRetainInstance(true); on OnCreate function of DialogFragment. This will cause dialog to dismiss on rotation.
Just add this line to specific activity in your Manifest to solve this problem android:configChanges="orientation|screenSize|smallestScreenSize"
like this,
<activity
android:name=".PDFTools"
android:exported="false"
android:configChanges="orientation|screenSize|smallestScreenSize"
android:theme="#style/Theme.DocScanner.NoActionBar" />

Categories

Resources