What's the best way to access the database through an async task?
I don't think I should pass in a reference to the DbAdapter that the activity is using (could be closed as the activity may be garbage-collected).
Also, the db needs a context to be opened and closed, but I don't have that with the async task.
AsyncTask is an abstract class, so you have to extend it.
Create a class which extends AsyncTask, then in the constructor of this class you can add every things you want like the Context.
Related
Hey guys so i want to use getReadableDatabase() in my main activity but the problem is that it is already extending appCompatActivity() and i need it to do that for my code. But now since i cant extend 2 things at a time ,i cant extend SQLiteOpenHelper and that leads to not being able to use getreadable. Can anyone help me with this issue?
You need to define create a second class that extends SQLiteOpenHelper and define methods within there that use getReadableDatabase(). You can set up SQLiteOpenHelper as a singleton so that you can access a single instance of the class from anywhere.
See http://blog.anorakgirl.co.uk/2018/01/refactoring-sqliteopenhelper-so-it-isnt-a-mile-long/ for how you can structure your SQLiteOpenHelper class.
I'm wondering where I should place my AsyncTask in my android project. As of right now I'm implementing an AsyncTask as a private class of my activity its running under. What I am going to do is in each activity that has a network call I will implement its own private class of AsyncTask. I have a few questions though
In The preexecute method it says I can interact with the activity and place a spinner or progress bar. I do this by using My_Activity_Class_Name.this. So my question is does that line of code reference the activity the AsyncTask is called from? If so I believe that will be a static method. How do i actually pass in the instance of the class so I can interact with non static functions?
I want to place all my Async code into one class for its respective needs. My quesiotn though is if i need to return a type back to the class that calls the Async method how can I return a value? Also is this the best practice?
You should make your inner private AsyncTask class - static. This is because otherwise it will have implicit reference to your Activity - this means that if your Activity will be recreated - ie. due to config change - then your AsyncTask will still hold your activity reference causing reference leak. This means you should pass reference to your activity to AsyncTask in ie. constructor of AsyncTask, and keep it in WeakReference (WeakReference/AsyncTask pattern in android).
So my question is does that line of code reference the activity the ASYNC Task is called from?
yes, but only if your AsyncTask class is non static
If so I beleve that will be a static method. How do i actually pass in the instance of the class so i can interact with non static functions?
its not a static method, with My_Activity_Class_Name.this you can access non static methods of your Activity class.
My quesiotn though is if i need to return a type back to the class that calls the Async method how can I return a value? Also is this the best practice?
You can call a method on your Activity class, there is nothing wrong with that. Remember that you cannot access UI widgets from non UI thread. So update your Activity widgets from onPostExecute which is called on UI thread.
when I made an SQLite database for an Android app I made a helper class that does not extend any other class. it is just to set up the database, in this example it is called PlayGame. it has a private class inside it shown here:
private static class DbHelper extends SQLiteOpenHelper
when I use the database for any reason in the main UI class called SQLiteExample I have to create an instance every time like this,
PlayGame entry = new PlayGame(SQLiteExample.this);
entry.open();
entry.createEntry(name, hits);
entry.close();
I am creating many instances of this database class called PlayGame in the other Activity class. Like in most all of my methods have to make instances to do the required function like read information into or out of the database.
so I read in best practices of android documentation that creating instances is heavy on memory and is best avoided. If this is the case is there a better way to do this? and does my example look like a bad use of memory?
Use singleton for every instance you want to use. instead of create a new instance of any object you will use the same object.
http://en.wikipedia.org/wiki/Singleton_pattern
I post here because I have a difficult question.
I have a class that extends TabNewsActivity of Activity
This class contains a nested class TabNewsActivity: DownloadData which extends to AsyncTask >>
This class TabNewsActivity displays the recovered data from my web service, a spot DownloadData is asynchronous which allows me to retrieve the values โโof my web service in a list.
To perform an update values โโ(in my application => refresh) I have to do this:
DownloadData (). Execute ();
But I can not do it out of my context TabNewsActivity: s
I would like a way to re execute this command, but in another tab for example.
Thank you for your help
Suggest making your DownloadData subclass in a separate class file, not a nested class of TabNewsActivity. You can pass it a Handler to act as a completion callback perhaps. This way you can execute DownloadData from TabNewsActivity, and pass it a Handler to call in TabNewsActivity upon completion. The same could hold true when calling it from another class.
AsyncTask execute() method is static so you dont need an instance of anything to call it. You call it like this:
AsyncTask.execute();
That will run what you have in your doInBackground() method. As far as I know you should be able to call AsyncTask.execute() anywhere in your app as long as you import AsyncTask.
Use AsyncTask inside of a Service.
I am new to android development. I would like to accomplish a task described as follows:
A main activty which calls external class(the other class would extend AsyncTask) to parse xml and receive json by requesting to web service and starts a ProgressDialog.
The class performs xml and json parsing in its doInBackground method.
In the onPostExecute method after parsing is complete, dismiss the ProgressDialog that was set in the main activity.
I could do this by passing the ProgressDialog object to the parsing class and dismissing the same object in its onPostExecute method.
I think passing an instance of UI object as argument is not a good approach to program, I hope there must be some other ways to work around.
Please suggest.
Thank you
The easiest way to decouple these is to use an interface:
Define a call-back interface (let's call it WorkDoneListener) with a single method: workDone().
Declare your activity class to implement WorkDoneListener and implement workDone() to dismiss the dialog.
Define the AsyncTask's constructor to accept a WorkDoneListener. Stash the reference in a member field.
In onPostExecute, call the listener's workDone() method.
Ted's answer is what you should do if your AsyncTask is too big and you want to declare it in other file. However, keep in mind that usually you declare the AsyncTask inside your UI class:
public class YourActivity extends Activity{
private class YourAsyncTask extends AsynkTask<etc.>{
}
}
In fact, if you are using you AsyncTask from that activity only (I mean, if you are not using it anywhere else), declaring the AsyncTask as a inner class is a good design practice.