Good day, as the title say, anyone know how to implement a progress dialog while loading data from a CursorLoader within a fragment. can't find any example in this regard. Any link or guide on how to do it will be highly appreciated. Thank you
I think #Michal's solution would be good for showing an indeterminate ProgressDialog via ProgressDialog#setIndeterminate(true) so I've added a +1. I'm not sure adding a Fragment to a Fragment like this (SomeFragment adding DialogFragment..) is approved as I've come a cropper on SO before suggesting something similar. Plus, it's ok that the ProgressDialog is used here since ultimately it is a component of the fragment thus belongs under the fragment without needing to exist as a separate Fragment entity.
To expand on this answer, if you were wanting to provide a real-time progress update then I would suggest that after each "unit of work" (the lowest denominator of work you can count at the CursorLoader level) you should fire an event via the LocalBroadcastManger (keep it local, no one else needs to know) which your Fragment will be listening for.
On receiving the event under the Fragment's nested BroadcastReceiver#onReceive() method can get a reference to your Fragment's ProgressDialog and increment the displayed progress with incrementProgressBy(diff) or similar.
If however, you just want to pop-up a "I'm doing something" dialog then setting the ProgressDialog to use setIndeterminate(true) will suffice.
Finally, have you considered using the pattern of adding the indeterminate progress dialog to the ActionBar? That is how a lot of the core apps operate whilst an underlying CursorLoader is working. It would be nice to keep it consistent. Check out items pertaining to requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS).
Cheers.
Update
To achieve the last approach you need to set-up your parent activity (the bit owning the ActionBar) with code similar to (I'm just writing this from memory!);
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Note that this is requested before you have called setContentView...
getWindow().requestFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.yourLayout);
At this point you have said, "I would like that spinny progress thing in my Activity ActionBar". Now depending on your activity implementation you can choose to show the indeterminate progress bar immediately in onCreate by writing;
setProgressBarIndeterminateVisibility(true);
But this may be too simplistic if an additional action causes the CursorLoader to be started. What is important to note throughout this exercise is that the progress dialog in the ActionBar is a feature of the owning activity and not your underlying Fragment. You don't want your fragment assuming that the INDETERMINATE_PROGRESS feature has been requested since (a) it may not have and (b) it's not it's prerogative to understand such things. In other words if you find yourself writing getActivity().setProgressBarIndeterminateVisibility(true) stop and think.
I think you should leverage a more decoupled approach where the underlying Fragments says, "I have started to perform a load" so in your CursorLoader callback, onCreateLoader something like;
#Override
public Loader<Result> onCreateLoader(int id, Bundle b) {
// Fire event saying this load is starting.
final Intent loadStarted = new Intent();
loadStarted.setAction(YourFragment.LOAD_STARTED);
return new SomeCursorLoader(this.getActivity());
}
Your activity can be listening for this event and then when it receives it, sets the indeterminate progress bar visibility to true.
Similiarly, when the CursorLoader callback, onLoaderFinished is called then fire another event like;
#Override
public void onLoadFinished(Loader<Result> loader, Result data) {
// Fire event saying this load is finished.
final Intent loadFinished = new Intent();
loadFinished.setAction(YourFragment.LOAD_FINISHED);
}
Finally your activity can then sets the indeterminate progress bar visibility to false onReceivieing this event and the cursor results are shown to the user...
I think you can implement LoaderCallback in your Fragment there you can use
callback methods like onCreateLoader to show dialog, and onLoadFinished to dismiss dialog. Look at code below:
public class SomeFragment extends Fragment implements LoaderCallbacks<Result> {
private DialogFragment dialog;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//you should implement yours DialogFragment
dialog = new DialogFragment();
//Start loader
getLoaderManager().initLoader(0, null, this);
}
#Override
public Loader<Result> onCreateLoader(int id, Bundle b) {
//Show dialog
dialog.show(getFragmentManager(), "TAG");
return new SomeCursorLoader(this.getActivity());
}
#Override
public void onLoadFinished(Loader<Result> loader, Result data) {
handler.sendEmptyMessage(0);
}
#Override
public void onLoaderReset(Loader<Result> arg0) {
}
private Handler handler = new Handler() {
public void handleMessage(Message msg) {
dialog.dismiss();
}
};
}
Related
The main activity has a "To ListView" button that launches a new activity with a list view. Each time this new activity loads, it calls an AsyncTask() method that retrieves some JSON remotely, parses it, and binds the data to the list view. Then, setContentView() is called (in onPostExecute()) to show the UI. How do I preserve the list view data, or at least the data array (for rebinding) on subsequent launches of that activity so that the ASyncTask() doesn't have to be called every time?
The ASyncTask() should get called only at the beginning (and not until when the application is forcefully terminated), subsequent calls should be done manually by the user perhaps with an onClick() event. I have tried setting a boolean for that purpose, but if so how to display the previous state of the list view when the boolean is false? I have also looked into onResume() and onBackPressed() but they don't seem to be much relevant.
Main.java:
toListView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent myIntent = new Intent(getApplicationContext(), ListView.class);
startActivity(myIntent);
}
});
ListView.java:
public class ListView extends ActionBarActivity {
private static boolean isFirstLaunch = true;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (isFirstLaunch) {
// execute the ASyncTask();
isFirstLaunch = false;
}
// else display previous listview data or last activity state
}// onCreate()
For persisting data in your Android application, you have two obvious choices.
Using an SQLite database. This alternative is good if you have quite a lot of data to manage, sort and maintain. E.g if your JSON response contains a lot of data, persisting it to a database would be a good solution. Then you would simply query the database instead of executing the AsyncTask repeatedly. Vogella provides an excellent tutorial on this matter.
Using SharedPerefences. This alternative is better if you only have a couple of variables to relate to. Storing the data in SharedPreferences relieves you of the work needed to design and implement database support for you application. See the official documentation for a reference.
I'm fetching data in my activity that is needed by several fragments. After the data is returned, I create the fragments. I was doing this via an AsyncTask, but it led to occasional crashes if the data returned after a screen rotation or the app is backgrounded.
I read up and thought the solution to this was instead using an AsyncTaskLoader. Supposedly it won't callback if your activity's gone, so those errors should be solved. But this now crashes every time because "Can not perform this action (add fragment) inside of onLoadFinished".
How am I supposed to handle this? I don't want my fragments to each have to fetch the data, so it seems like the activity is the right place to put the code.
Thanks!
Edit 1
Here's the relevant code. I don't think the problem is with the code per-se, but more of my whole approach. The exception is pretty clear I shouldn't be creating fragments when I am. I'm just not sure how to do this otherwise.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportLoaderManager().initLoader(BREWERY_LOADER, null, this).forceLoad();
}
//================================================================================
// Loader handlers
//================================================================================
#Override
public Loader<Brewery> onCreateLoader(int id, Bundle args) {
int breweryId = getIntent().getIntExtra(EXTRA_BREWERY_ID, -1);
return new BreweryLoader(this, breweryId);
}
#Override
public void onLoadFinished(Loader<Brewery> loader, Brewery data) {
if (data != null) {
onBreweryReceived(data);
} else {
onBreweryError();
}
}
#Override
public void onLoaderReset(Loader<Brewery> loader) {
}
...
protected void onBreweryReceived(Brewery brewery) {
...
createFragments();
}
...
protected void createFragments() {
FragmentManager fm = getSupportFragmentManager();
//beers fragment
mBeersFragment = (BreweryBeersFragment)fm.findFragmentById(R.id.beersFragmentContainer);
if (mBeersFragment == null) {
mBeersFragment = new BreweryBeersFragment();
fm.beginTransaction()
.add(R.id.beersFragmentContainer, mBeersFragment)
.commit();
Bundle beersBundle = new Bundle();
beersBundle.putInt(BreweryBeersFragment.EXTRA_BREWERY_ID, mBrewery.getId());
mBeersFragment.setArguments(beersBundle);
}
}
Edit 2
My new strategy is to use an IntentService with a ResultReceiver. I null out callbacks in onPause so there's no danger of my activity being hit when it shouldn't be. This feels a lot more heavy-handed than necessary, but AsyncTask and AsyncTaskLoader neither seemed to have everything I needed. Creating fragments in those callback methods doesn't seem to bother Android either.
From the MVC (Model -- View -- Controller) viewpoint, both the Activity and its fragments are Controller, while it is Model that should be responsible for loading data. As to the View, it is defined by the layout xml, you can define custom View classes, but usually you don't.
So create a Model class. Model is responsible for what must survive a screen turn. (Likely, it will be a static singleton; note that Android can kill and re-create the process, so the singleton may get set to null.) Note that Activities use Bundles to send data to themselves in the future.
I want to launch a dialog with a custom layout, which I've implemented via a DialogFragment. (I basically just changed onCreateView() and added button handlers). The dialog lets the user quickly change an important setting.
This dialog will be launched from several different activities. The different activities don't have much in common, except that they need to refresh after the user makes a change to the setting. They don't need to get any information from the dialog; they merely need to know when it's closed (dismissed).
What I've Tried
I tried having the activity refresh in onResume(), but launching and dismissing a dialog never seems to call this method. (So I'm not sure why it even exists, but that's probably a topic for another question.)
Next, I tried adding a DialogInterface.OnDismissListener to the dialog:
public static void showMyDialog(OnDismissListener listener, Activity activity)
{
DialogFragment fragment = new MyDialogFragment();
fragment.show(activity.getFragmentManager(), "date");
activity.getFragmentManager().executePendingTransactions();//A
fragment.getDialog().setOnDismissListener(listener);//B
}
When I originally left out the line A, I got a NullPointerException on line B because the dialog is null at that point. Following the advice of this SO answer, I put in the call to executePendingTransaction(). This causes an IllegalStateException on line B, with the message "OnDismissListener is already taken by DialogFragment and cannot be replaced." I also tried putting setOnDismissListener() before the call to show(), but that always caused a NullPointerException.
I then read this other SO answer, which says the original asker was "calling getDialog() too early in the DialogFragment's life cycle." So I tried adding a constructor to my DialogFragment:
public MyDialogFragment(SomeCallback illTakeAnythingICanGet)
{
//I'll store the callback here and call it later
}
Unfortunately, adding a constructor made Android Lint freak out with a fatal warning, and when I looked it up, I found a comment in this question that seems to say this approach will make it impossible to deal with the user rotating the screen while the dialog is open.
The Question
How can an activity figure out when a DialogFragment has closed (been dismissed) in a way that won't break my app if the user rotates the screen? Should I be using something else besides a DialogFragment?
This is just a longer explanation of harism's comment in case anyone else has the same problem I did.
You can accomplish what I wanted by creating an interface like this:
public interface MyDialogCloseListener
{
public void handleDialogClose(DialogInterface dialog);//or whatever args you want
}
Have the activity that launches your dialog (DialogFragment) implement this interface. Then give that DialogFragment the following method:
public void onDismiss(DialogInterface dialog)
{
Activity activity = getActivity();
if(activity instanceof MyDialogCloseListener)
((MyDialogCloseListener)activity).handleDialogClose(dialog);
}
More explanatory code for someone to do the same.
Create the interface as:
package com.example.dialoglistener;
import android.content.DialogInterface;
public interface MyDialogCloseListener {
public void handleDialogClose(DialogInterface dialog);
}
Implement the interface in activity as:
MyDialogCloseListener closeListener = new MyDialogCloseListener() {
#Override
public void handleDialogClose(DialogInterface dialog) {
//do here whatever you want to do on Dialog dismiss
}
};
Write a DismissListener in DialogFragement as
public void DismissListener(MyDialogCloseListener closeListener) {
this.closeListener = closeListener;
}
call DismissListener from your activity as:
dialogFragementObject.DismissListener(closeListener);
and finally write onDismiss method
#Override
public void onDismiss(DialogInterface dialog) {
super.onDismiss(dialog);
if(closeListener != null) {
closeListener.handleDialogClose(null);
}
}
Tyler's example was the only example I could find that actually worked. The only thing that needs changed for the example to work is the call to the DismissListner method in the DialogFragment class. He has it as:
dialogFragementObject.DismissListner(closeListener);
This just needs to be a cast to whatever your class name of that DialogFragment is. For example:
((MyDialogFragment)dialogFragementObject).DismissListner(closeListener);
I have had this "issue" for sometime on my app and it always bothered me that I didn't understand it's behavior, but never cared about asking why, until now.
I have a ListView (implemented through a ListActivity) with a custom layout for each list item. With that I have a custom CursorAdapter to properly fill all the list item elements (overriding newView and bindView, the ViewHolder pattern is already guaranteed when doing it like this, refer to: https://codereview.stackexchange.com/q/1057 if needed).
This is the main Activity and there are 2 actions that can update the content of the ListView, create a new item or edit a current one. For both, the same activity is used (which adapts itself to the type of action), something like this:
startActivityForResult(intent, ACTIVITY_NOTE_EDITOR_CREATE);
startActivityForResult(intent, ACTIVITY_NOTE_EDITOR_EDIT);
What actually distinguishes the action type is the intent contents, the requestCode is ignored in the sub activity.
Now, the documentation has this to say about the method above:
When this activity exits, your onActivityResult() method will be
called with the given requestCode.
But I'm not using onActivityResult. Actually, I'm not doing anything when the sub activity exists (either in create or edit mode). And the behavior I've noticed so far is this:
Create: Almost all the time I return to the main activity, the ListView is automatically updated with the new item. However, sometimes, rarely though, the ListView is not populated with the new item. I need to destroy the main activity (exiting the app for instance) and recreate it and then the new item will be there.
Edit: I never noticed similar behavior as just described for the create action. So far, the ListView was always automatically updated with the new content changes.
My question is, what's happening behind the scenes that makes my ListView to automatically update it's contents when I never call cursor.requery() nor manually notify of any changes to the content? And why the hell doesn't it work all the time?
Should I maybe force a cursor.requery() myself on onActivityResult?
public class MainActivity extends ListActivity {
private static final int ACTIVITY_NOTE_EDITOR_CREATE = 0;
private NotificationHelper mNotificationHelper;
private AgendaNotesAdapter mAgendaAdapter;
private Cursor mAllNotesCursor;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mNotificationHelper = Program.getNotificationHelper();
mAgendaAdapter = new AgendaNotesAdapter(this);
mAgendaAdapter.open();
initializeNotesListActivity();
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
Intent intent;
switch(item.getItemId()) {
case R.id.actionbar_item_new_note:
intent = NoteEditorMainActivity.createIntent(this);
startActivityForResult(intent, ACTIVITY_NOTE_EDITOR_CREATE);
return true;
}
return false;
}
private void initializeNotesListActivity() {
mAllNotesCursor = mAgendaAdapter.fetchAllNotes();
startManagingCursor(mAllNotesCursor);
NotesListAdapter notesAdapter = new NotesListAdapter(this, mAllNotesCursor,
mNotificationHelper, mAgendaAdapter);
setListAdapter(notesAdapter);
}
}
P.S: I know about CursorLoader and it's something in the roadmap, but I don't have time to deal with such code rewrites for the time being. Just thought I'd let you know. For now, I just need to know what is happening with my current code.
Your call to startManagingCursor() means that when the activity is stopped, the Cursor is deactivated, and when the activity is restarted, the Cursor is requeried.
Should I maybe force a cursor.requery() myself on onActivityResult?
This should automatically occur for your managed Cursor.
In my activity I need a ProgressDialog with a horizontal progress bar to visualise the progress of a background task. To make the activity care for the dialog e.g. in case of screen rotation, I would like to use a managed dialog created in onCreateDialog. The problem is that I need to update the progress bar of the dialog after it has been created and therefor I need a reference to the managed progress dialog: Does anyone know how to retrieve a reference to a dialog created by onCreateDialog?
At the moment I am storing a reference to the dialog created in onCreateDialog, but that my fail with a InvalidArgumentException in the onFinished() method after the screen has been rotated (and the activity has been recreated):
public final class MyActivity extends Activity {
private static final int DIALOG_PROGRESS = 0;
private ProgressDialog progressDialog = null;
// [...]
#Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DIALOG_PROGRESS:
progressDialog = createProgressDialog();
return progressDialog;
default:
return super.onCreateDialog(id);
}
}
// [...]
public void updateProgress(int progress) {
progressDialog.setProgress(0);
}
public void onFinished() {
progressDialog.dismiss();
}
// [...]
}
I would have expected something like a getDialog(int) method in the Activity class to get a reference to a managed dialog, but this doesn't seem to exist. Any ideas?
I answer myself:
There really is no getDialog(int) method available in the Activity class.
Storing the reference like shown above works correctly -- the bug was something else...
The problem was, that the parallel thread, that called the onFinished() method called this method on the already destroyed activity, thus the accessed ProgressDialog instance is still existing but no longer a valid dialog. Instead another activity with another ProgressDialog has already been created by Android.
So all I needed to do was to make the background thread call the onFinished() method of the new activity and everything works fine. To switch the reference I override the onRetainNonConfigurationInstance() and getLastNonConfigurationInstance() methods of the Activity class.
The good thing of the shown example: Android really cares about recreating the new dialog after the screen orientation changed. So constructing the ProgressDialog that way is definitely easier than using ProgressDialog.show() where I would need to handle the dialog recreation on my own (the two methods described above would be a good place to do this.