Question

Setup: FirstActivity opens SecondActivity, SecondActivity asks user to select images, give them titles; when user is finished they click "Save" button that invokes method saveToDatabase which calls for a bulk insert. The desire is to have the user return to FirstActivity after they click "Save" button.

This is the current setup:

private void saveToDatabase() {
    int arraySize = beanList.size();
    ContentValues[] valuesArray = new ContentValues[arraySize];

    ContentValues values;
    String imageuri;
    String title;
    int counter = 0;

    for(Bean b : beanList){
        imageuri = b.getImageUri();
        title = b.getImageTitle();

        values = new ContentValues();           
        values.put(CollectionsTable.COL_NAME, nameOfCollection);
        values.put(CollectionsTable.COL_IMAGEURI, imageuri);
        values.put(CollectionsTable.COL_TITLE, title);
        values.put(CollectionsTable.COL_SEQ, counter +1);

        valuesArray[counter] = values;
        counter++;
    }
    getContentResolver().bulkInsert(CollectionsContentProvider.CONTENT_URI, valuesArray);   

    // Does this squash the db/provider call? This is working now but will it always?       
    finish();               
}

...and it is working but I have a lingering fear that it may not work all the time. So my question is, is calling finish() directly after getContentResolver().bulk... an issue? Is there a better way to handle what I'm trying to do (save to database & return user to previous activity in response to one user event)? Thank you. PS: This is my first app, if you see code that should be handled better I'm all ears on that, too.

Was it helpful?

Solution

So the main question was: is it okay to call finish() directly after a Provider method call. And given makovkastar's comments, the solution is not to call finish() directly after the Provider method call but to instead do something like:

private void saveToDatabase() {
    int arraySize = beanList.size();
    final ContentValues[] valuesArray = new ContentValues[arraySize];

    ContentValues values;
    String imageuri;
    String title;
    int counter = 0;

    for(Bean b : beanList){
        imageuri = b.getImageUri();
        title = b.getImageTitle();

        values = new ContentValues();           
        values.put(CollectionsTable.COL_NAME, nameOfCollection);
        values.put(CollectionsTable.COL_IMAGEURI, imageuri);
        values.put(CollectionsTable.COL_TITLE, title);
        values.put(CollectionsTable.COL_SEQ, counter +1);

        valuesArray[counter] = values;
        counter++;
    }

    AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {

        @Override
        protected Void doInBackground(Void... arg0) {
            getContentResolver().bulkInsert(CollectionsContentProvider.CONTENT_URI, valuesArray);   
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            finish();   
        }           
    };

    task.execute();                 
}

This works and now my previous fears are allayed. Thanks, makovkastar.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top