Fragment and Service are working with same resources - android

I'm making android app in which user can work with small images.
App has a backend (parse.com) where images located, so I can add and delete some images.
On the client side images store on SD (files) and there is sqlite table where filepaths located.
App has a Fragment that takes all available images and shows them in GridView with ImageViews. I'm using SQLiteCursorLoader/CursorAdapter to get filepaths and then just load bitmaps to ImageViews.
Also I have IntentService, that sync images with backend by timer. Its load and save to SD new images and remove files of images, that were deleted. Its also modify sqlite table with filepaths of images.
The problem is: IntentService can start doing his job in the same time when user works with Fragment. User can choose image, that has been already deleted by IntentService.
Is there any possibility for IntentService to check that Fragment (or other data consumers) working right now? Should IntentService stop doing his job in that case?
Are there a 'good practices' of solving this kind of issues?
I will be grateful for any help

My preferred way to keep your UI up to date with your database is to implement the content obversable pattern using a Content Provider and observe the changes using the Loader Manager.
Your fragment will look something like this:
public class SomeFragment extends Fragment
implements LoaderManager.LoaderCallbacks<Cursor> {
private CursorAdapter mAdapter;
// ...
#Override public void onViewCreated(View view) {
// ...
mAdapter = new ResourceCursorAdapter(getActivity(), R.layout.my_layout, null, false);
// attach adapter to your grid-view...
getLoaderManager().initLoader(0, null, this);
}
#Override public Loader<Cursor> onCreateLoader(int id, Bundle args) {
return new CursorLoader(getActivity(), MY_IMAGE_MODEL_URI, null, null, null, null);
}
#Override public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
// this automatically reloads your adapter and updates your grid-view
mAdapter.swapCursor(cursor);
}
#Override public void onLoaderReset(Loader<Cursor> loader) {
mAdapter.swapCursor(null);
}
}
Note that the Uri that is passed to the CursorLoader will be observed for changes. When a change is detected, the Loader will query the specified Uri after which onLoadFinished is called. But it can only detect changes if you notify the system that a change took place on that Uri. You typically do that inside your content provider, after you have done an update, insert or delete. But it could also be done by your IntentService after it has deleted, inserted or updated your data.
The code to do that will look something like this:
getContentResolver().notifyChange(MY_IMAGE_MODEL_URI, null);

in fragment class Create a public static variable like
public static boolean active = false;
and oAcivityCreated method make it true
onpause or onStop make it false
you can this variable from IntentService
example
if (myfragment.active){
//do Somthing if fragment is running
}else{
//Somthing if not running
}
ithink it will work for you

Related

Refresh Recyclerview from Broadcast receiver

I have a notification which contains a next button and a previous button. Now when I press any of the buttons in the notification the actions are received by the Broadcast receiver.
What my problem is that I am having trouble refreshing my recyclerview when either of the buttons are pressed.
I have been able to do it if I would set my adapter to static in the main class and call it in the broadcast receiver it works like this:
BroadCast Receiver Class:
public void getPlaylistItems(){
if (list != null){
list.clear();
}
SongsDatabase SongsDB = new SongsDatabase(contexts);
Cursor data = SongsDB .getSongs();
if(data.getCount() != 0){
data.moveToFirst();
do{
Songs myList = new (data.getString(0), data.getString(1), data.getString(2), data.getString(3), data.getString(4));
list.add(myList);
} while (data.moveToNext());
}
data.close();
VideoDB.close();
adapter.notifyDataSetChanged <--- This was made static in my Main Activity which contains the recyclerview
}
The problem with this solution is that it causes a memory leak in my app.
Now the question is how would I be able to refresh my recyclerview without making any variables static?
I believe you are creating an inner class inside your activity/fragment. To avoid making adapter static, you can create this broadcast receiver inside on your onCreate function instead.
Other approaches could be using an Event Bus,Interfaces or RxJava.
Yes you can without having a static reference to the adapter of your RecyclerView. You have the ContentObserver for that.
As I can see that, you are fetching the songs and the videos from the local database, you can easily put a content observer along with your database table. In that case, you need to create an Uri first which might look something like this.
public static final Uri DB_TABLE_SONGS_URI = Uri
.parse("sqlite://" + Constants.ApplicationPackage + "/" + DB_TABLE_SONGS);
// DB_TABLE_SONGS refers to the database table that you have for storing songs.
Now, while updating or inserting a new row in the songs table you need to notify the content observer like this.
context.getContentResolver().notifyChange(DBConstants.DB_TABLE_SONGS_URI, null);
Now use LoaderCallbacks and LoaderManager to fetch the data from songs table. Hence, in onCreateLoader function you need to register the content observer which will aid to recycle your RecyclerView without having any static reference to it.
This is a sample onCreateLoader function.
#Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
return new SQLiteCursorLoader(getActivity()) {
#Override
public Cursor loadInBackground() {
SongsDatabase SongsDB = new SongsDatabase(contexts);
cursor = SongsDB.getSongs();
this.registerContentObserver(cursor, DBConstants.DB_TABLE_SONGS_URI);
}
return cursor;
}
};
}
Do not forget to destroy the loader instance in onDestroy function.
getLoaderManager().destroyLoader(SONGS_QUERY_LOADER);

In Android, is there a way to get notified when a contact's photo changes (via receiver, listener, etc)?

My app uses a custom layout do display all the contact info, including photos. It's designed to handle a very large amount of contacts (in the 1000s), and I've created a cache for the contact photos to make them load much faster. However, I'm running into issues when contact photos are updated on the phone. Since I'm using cached ones, the new photo doesn't show up in the app until it's force-closed and then restarted. And I don't want to use anything like a last-accessed timestamp to refresh the cached photos because doing so would significantly slow down the app every time a refresh is needed.
So, ideally, I would like to get notified (via something like a broadcast receiver) whenever a contact photo is changed, and I can then update that photo in the cache. Is that possible?
user CursorLoader instead to update your gui automatically. Here you can see a tutorial. Obviously there are plenty of other examples out there.
You can use ContentObserver.
public class ContractObserv extends Activity {
MyContentObserver observer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
observer=new MyContentObserver();
getApplicationContext().getContentResolver().registerContentObserver(ContactsContract.Contacts.CONTENT_URI, true, observer);
}
private class MyContentObserver extends ContentObserver {
public MyContentObserver() {
super(null);
}
#Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
Log.d("Androider", "INSIDE ONcHANGE");
}
}}
Its amazing that once you run it and every time a contact changes, the OnChange is called until you unregister it.

How do CursorLoader automatically updates the view even if the app is inactive?

I have been working on a small To-Do list app. I used CursorLoader to update the ToDolistview from a content provider. I have a written a function onNewItemAdded(), which is called when user enters a new item in the text view and clicks enter. Refer below:
public void onNewItemAdded(String newItem) {
ContentResolver cr = getContentResolver();
ContentValues values = new ContentValues();
values.put(ToDoContentProvider.KEY_TASK, newItem);
cr.insert(ToDoContentProvider.CONTENT_URI, values);
// getLoaderManager().restartLoader(0, null, this); // commented for the sake of testing
}
#Override
protected void onResume() {
super.onResume();
//getLoaderManager().restartLoader(0, null, this); // commented for the sake of testing
}
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
CursorLoader loader = new CursorLoader(this,
ToDoContentProvider.CONTENT_URI, null, null, null, null);
Log.e("GOPAL", "In the onCreateLoader");
return loader;
}
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
int keyTaskIndex = cursor.getColumnIndexOrThrow(ToDoContentProvider.KEY_TASK);
Log.e("GOPAL", "In the onLoadFinished");
todoItems.clear();
if (cursor.moveToNext() == false) Log.e("GOPAL", "Empty Cursor");
else {
while (cursor.moveToNext()) {
ToDoItem newItem = new ToDoItem(cursor.getString(keyTaskIndex));
todoItems.add(newItem);
}
aa.notifyDataSetChanged(); // aa is arrayadapter used for the listview
}
}
I have read, CursorLoader automatically updates the view, whenever there is a data change in the content provider db. That means I suppose, getLoaderManager().restartLoader(0, null, this) has to be called implicitly whenever there is a change in data, right?
But that is not happening. Whenever I add a new item (the item is added to the db from onNewItemAdded, but restartLoader is not explicitly called), pause this activity and resume it back. I don't see any implicit call to restartLoader(even though db is changed) and the listview also is not updated with new item added. Why is that? How does a CursorLoader automatically updates the view even if app is not active???
Thanks :)
EDIT: I have also used getContext().getContentResolver().notifyChange(insertedId, null) in insert of my content provider.
I found the answer for my question. In general, CursorLoader doesn't automatically detect data changes and load them to view. We need to track URI for changes. This can be done by following steps:
Registering an Observer in content resolver through cursor using: (Done in the query method of ContentProvider)
cursor.setNotificationUri(getContext().getContentResolver(), uri);
Now when there is any change in URI underlying data using insert()/delete()/update(), we notify the ContentResolver about the change using:
getContext().getContentResolver().notifyChange(insertedId, null);
This is received by the observer, we registered in step-1 and this calls to ContentResolver.query(), which inturn calls ContentProvider's query() method to return a fresh cursor to LoaderManager. LoaderManager calls onLoadFinished() passing this cursor, along with the CursorLoader where we update the View (using Adapter.swapCursor()) with fresh data.
For Custom AsyncTaskLoaders:
At times we need our custom loader instead of CursorLoader. Here we can use someother object other than cursor to point to the loaded data (like list etc). In this we won't be having previlige to notify ContentResolver through cursor. The application may also not have a content Provider, to track URI changes. In this scenario we use BroadcastReceiver or explicit ContentObserver to achieve automatic view updation. This is as follows:
We need to define our custom loader which extends AsyncTaskLoader and implements all its abstract methods. Unlike CursorLoader, our Custom Loader may or may not use a content Provider and it's constructor may not call to ContentResolver.query(), when this loader is instatiated. So we use a broadcast receiver to serve the purpose.
We need to instantiate a BroadCastReceiver or ContentObserver in OnStartLoading() method of abstract AsyncTaskLoader class.
This BroadCast receiver should be defined to receive data-changing broadcasts from content provider or any system events(Like new app installed) and it has to call loader's onContentChanged() method, to notify the loader about the data change. Loader automatically does the rest to load the updated data and call onLoadFinished() to update the view.
For more details refer this: http://developer.android.com/reference/android/content/AsyncTaskLoader.html
I found this very useful for clear explanation : http://www.androiddesignpatterns.com/2012/08/implementing-loaders.html
Well, I think you can restart the loader on certain events. E.g. in my case I have an activity of TODOs. On clicking 'add' option, it launches new activity which has view to feed new TODO.
I am using following code in parent activity's onActivityResult()
getLoaderManager().restartLoader(0, null, this);
It works fine for me. Please share if there is any better approach.
get a reference to your loader while initializing as follows
Loader dayWeatherLoader = getLoaderManager().initLoader(LOADER_DAY_WEATHER, null, this);
then create a class that extends ContentObserver as follows
class DataObserver extends ContentObserver {
public DataObserver(Handler handler) {
super(handler);
}
#Override
public void onChange(boolean selfChange, Uri uri) {
dayWeatherLoader.forceLoad();
}
}
Then register content observer inside onResume lifecycle method as follows
#Override
public void onResume() {
super.onResume();
getContext().getContentResolver().registerContentObserver(CONTENTPROVIDERURI,true,new DayWeatherDataObserver(new Handler()));
}
Whenever there is a change in the underlying data of content provider, the onChange method of contentobserver will be called where you can ask loader to load the data again

Android Combine ImageDownload with CursorLoader

I am using a very simple CursorLoader with an ImageDownloader. The ImageDownloader is running and everything but the CursorLoader finishes, and THEN the ImageDownloader begins its job of downloading the images, but the GridView is not updating with the downloaded images..
Within my ListFragment I have the following onActivityCreated method:
#Override
public void onActivityCreated(Bundle savedInstanceState) {
Log.d(TAG, "onActivityCreated");
super.onActivityCreated(savedInstanceState);
cursorAdapter = new OURSVPImageAdapter(getActivity(), null);
// set the adapter on the gridview
mGridView.setAdapter(cursorAdapter);
// load the data
getActivity().getSupportLoaderManager().initLoader(2, null, this);
}
My CursorLoader is as follows:
public static final class PhotoCursorLoader extends SimpleCursorLoader {
Context mContext;
public PhotoCursorLoader(Context context) {
super(context);
Log.d("PhotoCursorLoader", "Constructor");
mContext = context;
}
#Override
public Cursor loadInBackground() {
Log.d("PhotoCursorLoader", "loadInBackground");
PhotosDataSource datasource = new PhotosDataSource(mContext);
# STEP 1
return datasource.getAllPhotos(((EventActivity) mContext).getEventId());
}
}
The line labeled # STEP 1 that retrieves all of the photos is just a method that retrieves a Cursor, as seen here:
public Cursor getAllPhotos(long event_id) {
Log.d(TAG, "getAllPhotos");
Cursor mCursor = getWritableDatabase().query(true, TABLE_NAME, COLUMNS_PHOTOS, DatabaseConstants.KEY_EVENT_ID + "=" + event_id,
null, null, null, null, null);
return mCursor;
}
So, this being the CursorLoader for the ListFragment, it assumes its complete when that Cursor is returned, which is correct. From my understanding the setAdapter() method is what would actually trigger the getView method.
The Trouble I'm having is that Everything seems to be running fine, my log output is outputting the urls for the images correctly, breakpoints all showing legit data, the issue though, is that my grid view never gets updated with the images that the ImageDownloader retrieves.
EDIT
This is the SimpleCursorLoader I'm using: https://gist.github.com/1217628
Once again, the problem is most likely that you are managing your Fragment's loaders from the parent Activity. The Activity and Fragment lifecycles do not sync with each other (at least not in a predictable manner), so your attempts at debugging the issue aren't actually getting you anywhere. The easiest way to guarantee predictable behavior is to have each separate component make use of its own LoaderManager (as opposed to having your Fragments access getActivity().getSupportLoaderManager()).
As with another of my questions.. the refresh shouldn't be forced, and when using a provider you can have the provider notify the contentresovler of updates. I have a method that inserts new photos, and that method uses the data provider. The data provider needed to
getContext().getContentResolver().notifyChange(uri, null);
to actually update the Cursor.

Getting SQLiteCursorLoader to observe data changes

I'm trying to implement a DataListFragment with an adapter that uses a Loader from Commonsware. This Loader uses a SQLiteDatabase directly and doesn't require the use of ContentProviders.
The android reference states about Loaders:
"While Loaders are active they should monitor the source of their data and deliver new results when the contents change."
Under my SQLiteCursor implementation (below), this does not happen. OnLoadFinished() gets called once and that's it. Presumably, one could insert Loader.onContentChanged() calls where the underlying database gets changed, but in general the database code class does not know about loaders, so I'm not sure about the best way to go about implementing this.
Does anyone have any advice on making the Loader "data aware", or should I wrap the database stuff in as a ContentProvider and use CursorLoader instead?
import com.commonsware.cwac.loaderex.SQLiteCursorLoader;
public class DataListFragment extends ListFragment implements LoaderManager.LoaderCallbacks<Cursor>{
protected DataListAdapter mAdapter; // This is the Adapter being used to display the list's data.
public SQLiteDatabase mSqlDb;
private static final int LOADER_ID = 1;
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
int rowlayoutID = getArguments().getInt("rowLayoutID");
// Create an empty adapter we will use to display the loaded data.
// We pass 0 to flags, since the Loader will watch for data changes
mAdapter = new DataListAdapter(getActivity(),rowlayoutID, null , 0);
setListAdapter(mAdapter);
// Prepare the loader. Either re-connect with an existing one,
// or start a new one.
LoaderManager lm = getLoaderManager();
// OnLoadFinished gets called after this, but never again.
lm.initLoader(LOADER_ID, null, this);
}
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
String sql="SELECT * FROM "+TABLE_NAME+";";
String[] params = null;
SQLiteCursorLoader CursorLoader = new SQLiteCursorLoader(getActivity(), mSqlDb, sql, params);
return CursorLoader;
}
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
// Swap the new cursor in. (The framework will take care of closing the old cursor once we return.)
mAdapter.swapCursor(data);
// The list should now be shown.
if (isResumed()) { setListShown(true);}
else { setListShownNoAnimation(true); }
}
public void onLoaderReset(Loader<Cursor> loader) {
// This is called when the last Cursor provided to onLoadFinished()
// above is about to be closed. We need to make sure we are no
// longer using it.
mAdapter.swapCursor(null);
}
The Loader documentation is flawed.
100% of Loader implementations built into Android itself "monitor the source of their data and deliver new results when the contents change". Since there is only one Loader implementation built into Android itself as of now, their documentation is accurate as far as that goes.
However, quoting a book update of mine that should be released in an hour or two:
There is nothing in the framework that requires this
behavior. Moreover, there are some cases where is clearly a bad idea to do
this – imagine a Loader loading data off of the Internet, needing to
constantly poll some server to look for changes.
I do plan on augmenting SQLiteCursorLoader to be at least a bit more aware of database changes, if you route all database modifications through it. That too will have limitations, because you don't share Loader objects between activities (let alone have access to them from services).
The only reason CursorLoader works as it does is because it uses a ContentProvider -- a singleton that can therefore be aware of all operations.
At the moment, whatever portion of your code is responsible for inserts, updates, and deletes will either need to tap the SQLiteCursorLoader on the shoulder and have it update, or notify the activity of the change (e.g., broadcast from a Service) so the activity can tap the SQLiteCursorLoader on the shoulder.

Categories

Resources