Calling Activity from Async Task - android

I am unable to call an activity from async task, i have tried different ways but every time i get a null pointer exception in the log cat.
I have tried this.
#Override
protected void onPostExecute(String result) {
if(signedin==false)
{
Toast.makeText(XMPPClient.context, "authentication failed:", Toast.LENGTH_LONG).show();
}
Intent i = new Intent(getApplicationContext(), ProfileHome.class);
startActivity(i);
}
and this
#Override
protected void onPostExecute(String result) {
XMPPClient xmpp = new XMPPClient();
if(signedin==false)
{
Toast.makeText(XMPPClient.context, "authentication failed:", Toast.LENGTH_LONG).show();
}else
xmpp.startnewactivity();
}
And in XMPPClient.class
public void startnewactivity()
{
Intent i = new Intent(getApplicationContext(), ProfileHome.class);
startActivity(i);
}
How can i call an activity from async task, actually I want to call activity when async task finishes.

I believe the problem is here:
Intent i = new Intent(getApplicationContext(), ProfileHome.class);
Usually, using getApplicationContext() as a Context is a very bad idea unless you really know what are you doing. Instead, you should use the Context of the Activity that you want to start the new from. You can achieve this passing it as a value on .execute() or even storing the Context you get in the AsyncTask's constructor and store it, so you later can use it in your onPostExecute() method.
---- EDIT ----
This would be an example:
public class MyAsyncTask extends AsyncTask {
Context context;
private MyAsyncTask(Context context) { this.context = context; }
#Override
protected void onPostExecute(...) {
super.onPostExecute(result);
context.startActivity(new Intent(context, ProfileHome.class));
}
}

Related

Pass different type as activity

I have several activities that need to perform HTTP requests (send a JSON request a get another JSON object back).
My idea was to share one AsyncTask for all these requests. I am passing the Activity as a parameter so that I can call method once the execution of the request is finished.
I would like to pass one more parameter to my AsyncTask that would be the class of my Activity (MainActivity.class, SecondActivity.class) and then use that information to cast the Activity to the correct type and then later call on the method (would be the same method name for all activities).
I could also create an interface with my call back method, but I am not sure if I that would work neither.
Could this work or is my approach wrong here ?
Thanks for your feed-back.
My code:
public class HTTPReq extends AsyncTask {
private MainActivity callerActivity;
#Override
protected String doInBackground(Object... params) {
String data = (String) params[0];
String cookie = (String) params[1];
callerActivity = (MainActivity) params[2];
...
}
#Override
protected void onPostExecute(String result) {
callerActivity.ProcessHTTPReqAnswer(result);
super.onPostExecute(result);
}
}
Aswins answer isn't terrible but it's still not the most efficient way.
Declare an Interface that has a method for callback. Pass an instance of that interface to your asynctask then have the async task invoke it if its there as per my examples below
Interface:
public interface IMyCallbackInterface {
void onCallback(String result);
}
Async Task:
public MyAsyncTask extends AsyncTask<..., String> {
private IMyCallbackInterface mCallback;
public MyAsyncTask(..., IMyCallbackInterface callback) {
mCallback = callback;
}
protected String doInBackground(Object... params) {
....
}
protected void onPostExecute(String result) {
super.onPostExecute(result);
if (mCallback != null) {
mCallback.onCallback(result);
}
}
Activity:
public MyActivity extends Activity {
private void someMethod(){
new MyAsyncTask(..., new IMyCallbackInterface() {
public void onCallback(String result) {
//TODO use the result to do whatever i need
//I have access to my aactivity methods and member variables here
}
}.execute();
}
}
It is a wrong approach to do it like that. You should use BroadcastReceiver. Once a AsyncTask is done, sent the result out as a Broadcast. Each of the activity will be listen to the results they are interested in. This way no one needs to keep a reference to the activity which is dangerous.
Here is an example.
IntentFilter filter = new IntentFilter();
filter.addAction("result1");
LocalBroadcastManager.getInstance(getSupportActivity()).registerReceiver(new CustomBroadcastReceiver(), filter);
In the AsyncTask, do this
#Override
protected void onPostExecute(String result) {
Intent intent = new Intent("result1").putExtra(
"data", result);
LocalBroadcastManager.getInstance(getSupportActivity()).sendBroadcast(intent);
super.onPostExecute(result);
}
Back in the activity, do this
private class CustomBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
if ("result1".equalsIgnoreCase(intent.getAction())) {
String result = bundle.getString("data");
// Process the result here.
}
}
}

Static call for startActivity from AsyncTask

I have implemented a FragmentPagerAdapter of 4-lashes, and in each of them I load a fragment with a different view. In one of them, pressing an image executed a AsyncTask to obtain a series of data from a server and loads a new class through an intent on the onPostExecute() method.
I had this functionality in one activity and worked perfectly. Now to make the call from the fragment I have to make calls using a static mode of this class and I get error in the line of code 'startActivity(i)':
//AsyncTask
private static class CargarJSON extends AsyncTask<String, Void, String> {
Context mContext;
public CargarJSON(Context context) {
mContext = context;
}
#Override
protected void onPreExecute() {
super.onPreExecute();
mProgressItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
mProgressItem.setVisible(true);
mProgressItem.setActionView(R.layout.actionbar_indeterminate_progress);
mProgressItem.expandActionView();
}
#Override
protected String doInBackground(String... params) {
String url = params[0];
String data = MetodosJSON.getHttpResponse(url);
MetodosJSON.parseaJSON2(data, IniSelCategoria.ac);
return params[1];
}
#Override
protected void onPostExecute(String titulo) {
super.onPostExecute(titulo);
// start new activity
Intent i = new Intent(mContext, PantallaInfo.class);
i.putExtra("title", titulo);
i.putExtra("URLser", urlSer);
**startActivity(i);**
mProgressItem.setVisible(false);
}
}
The mistake is:
Cannot make a static reference to the non-static method startActivity(Intent) from the type Activity
How do I make the method call 'startActivity(i)'?
Thank you very much.
Change it to
mContext.startActivity(i);
You need to use a context to call that method if not calling from an Activity. Luckily you are already passing a Context to the constructor.
Change your code with the below one.
Intent i = new Intent(mContext, PantallaInfo.class);
i.putExtra("title", titulo);
i.putExtra("URLser", urlSer);
mContext.startActivity(i); // call using Context instance

How update UI within AsyncTask

I want download details from web and update the UI within the doInBackground(),
For that I think I must get reference to activity within that method .How can I do it or is there another way to do that? What must be the something parameter? Or can’t update UI real-time?
public class DownloadActivity extends ListActivity {
public class DownloadItems extends AsyncTask<Something,Integer,Long> {
#Override
protected Long doInBackground(DownloadActivity... params) {
Toast.makeText(params[0], getIntent().getExtras().get("location").toString(), Toast.LENGTH_SHORT).show();
return null;
}
}
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
new DownloadItems().execute(Something);
}
}
You can either use a Handler or update your UI in onPostExecute(), which I recommend. Let your Async take care of its background logic and update the UI when that work is finished.
The best way is to simply move anything which affects UI into onPostExecute() because it's there to allow you to update the UI, it's the point of it.
There are other ways but when using AsyncTask there's really no reason not to use this.
public class DownloadActivity extends ListActivity {
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
new DownloadItems(this).execute();
}
public class DownloadItems extends AsyncTask<Something,Integer,Long> {
private Context context;
public DownloadItems(Context c){
context = c;
}
#Override
protected Long doInBackground(DownloadActivity... params) {
// Do something
return null;
}
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
Toast.makeText(context, context.getIntent().getExtras().get("location").toString(), Toast.LENGTH_SHORT).show();
}
}
}
You can create a constructor for passing or adding Context as a parameter.
public class DownloadItems extends AsyncTask<Something,Integer,Long> {
Context context;
public DownloadItems(Context cntx){
context = cntx;
}
#Override
protected Long doInBackground(DownloadActivity... params) {
//Toast.makeText(params[0], getIntent().getExtras().get("location").toString(), Toast.LENGTH_SHORT).show();
Toast.makeText(context, "String test", Toast.LENGTH_SHORT).show();
return null;
}
}
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
new DownloadItems(this).execute(Something);
}
By passing the context of the activity you can make any operation that are context related.
You can't execute UI operations in doInBackground(), you must do them in onPostExecute(). In DownloadActivity, you will create an instance of DownloadItems, and pass it the url where you want to download your stuff :
For example :
public class DownloadActivity extends ListActivity {
private void someMethod() {
DownloadItems yourTask = new DownloadItems(getApplicationContext());
yourTask.execute(yourUrl);
}
In the AsyncTask, you will do your download operations in doInBackground() and return the result so it can be handled by onPostExecute() :
public class DownloadItems extends AsyncTask<Something,Integer,Long> {
Context mContext;
public DownloadItems(Context context){
mContext = context;
}
#Override
protected String doInBackground(String... params) {
String theResult;
// download operations using url stored in params[0], and where you set theResult variable (for example...)
return theResult;
}
In onPostExecute(), you deal with the result, for example in your code above, you can call the Toast :
#Override
protected void onPostExecute(String result) {
Toast.makeText("YOUR TAG", result, Toast.LENGTH_SHORT).show();
}
You can call this in doInBackground:
runOnUiThread(new Runnable() {
public void run() {
//Your code
}
});
But isn't right... Please read the AsyncTask for more details, or use the onPostExecute to update UI...

How to correctly start activity from PostExecute in Android?

I have an AsyncTask, that fills a custom List with parsed data from Internet.
In PostExecute I fill that List and get it ready to transfer it to a new Activity.
I do it this way:
#Override
protected void onPostExecute(List<VideoDataDescription> result)
{
super.onPostExecute(result);
MainActivity.progressDialog.dismiss();
context.startActivity(new Intent(context, ResultsQueryActivity.class));
}
where context
private Context context;
In LogCat after executing this code I get a Java.lang.NullPointerException.
Is this possible and correct to start an Activity as I do it?
UPD
I have added
private Context mContext;
public YoutubeAndYahooParser(Context context)
{
super();
this.mContext = context;
}
to initialize context and call
YoutubeAndYahooParser youtubeAndYahooParser = new YoutubeAndYahooParser(ResultsQueryActivity.this);
youtubeAndYahooParser.execute("my string to pass in asynctak");
After this in PostExecute
Intent intent = new Intent(mContext, ResultsQueryActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(intent);
I added new flag because of I have got in LogCat the next:
*Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?*
Am I right?
You should pass in the application context rather than a context from the local activity. I.e. use context.getApplicationContext() and save that in a local variable in your AsyncTask subsclass.
The code might looks something like this:
public class MyAsyncTask extends AsyncTask {
Context context;
private MyAsyncTask(Context context) {
this.context = context.getApplicationContext();
}
#Override
protected Object doInBackground(Object... params) {
...
}
#Override
protected void onPostExecute(List<VideoDataDescription> result) {
super.onPostExecute(result);
MainActivity.progressDialog.dismiss();
context.startActivity(new Intent(context, ResultsQueryActivity.class));
}
}
you'd call it like this:
new MyAsyncTask(context).execute();
I tried this just now ... it works in PostExecute Method!!!
Intent intent_name = new Intent();
intent_name.setClass(getApplicationContext(),DestinationClassName.class);
startActivity(intent_name);
But its better if you start a new Intent Based on the response(result) obtained from the previous activities.
This will eliminate the possibility of the error response from invoking the new intent.
Example if the previous activity was supposed to return Succesfully... or Welcome to allow the new intent to start, the i could check it out in this way
protected void onPostExecute(String result) {
if (result.equals("Succesfully...")){
context.startActivity(new Intent(context, Login_Activity.class));
Toast.makeText(context, result, Toast.LENGTH_LONG).show();
}else if (result.contains("Welcome")){
context.startActivity(new Intent(context, MainActivity.class));
}else {
Toast.makeText(context,result,Toast.LENGTH_LONG).show();
}
}

Android Why does my app Force to close?

I admit, I'm new at this whole Android stuff. I am trying to make an app but randomly I get Force close errors and I really don't know why. My application has many activities, none of them finish() when I start a new one. I get data from the web (via web services and direct image downloading) and I use AsyncTask a lot. Most of the time it crashes on the asynctask. Here is a sample on how I do things:
private BackTask backTask;
Activity ctx = this;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.trackslist);
backTask = new BackTask();
backTask.execute();
}
protected class BackTask extends AsyncTask<Context, String, myObject>
{
#Override
protected myObject doInBackground(Context... params)
{
try{
if (hasInternet(ctx)==true)
{
//access the web SERVICE here
//initialize myObject WITH result FROM the web
return myObject
}
else
{
return null
}
}catch(Exception ex){
return null;
}
}
#Override
protected void onPreExecute()
{
super.onPreExecute();
}
#Override
protected void onProgressUpdate(String... values)
{
super.onProgressUpdate(values);
}
#Override
protected void onCancelled()
{
super.onCancelled();
}
#Override
protected void onPostExecute( myObject result )
{
super.onPostExecute(result);
if (result==null || result.isEmpty())
{
//no valid result, show a message
}
else
{
//result valid do something with it
}
}
}
#Override
public void onPause()
{
if (backTask!=null && ! backTask.isCancelled())
{
backTask.cancel(true);
}
super.onPause();
}
public void btnStartOnClick(View target) {
Intent intent = new Intent(this, MyNewActivity.class);
startActivity(intent);
}
When the activity gets onPause() the task is being canceled. I am not sure what happens during the try/catch if a error appears, from what I've did, it should return null, but I think here I miss something. As I said before, randomly I get a force close even if I am on another Activity. This is really frustrating as I can't offer a app that has this behavior. So, what am I doing wrong ?
There is problem in your code. I have corrected as follows: You find I have added this while calling async task.
Your async task accept context as argument and you was not passing that.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.trackslist);
backTask = new BackTask();
backTask.execute(this);
}
You need to ask inside your AsyncTask class for isCancelled() and then decide what to do.
Check this question. It has a good explanation by Romain Guy:
You can stop an AsyncTask. If you call
cancel(true), an interrupt will be
sent to the background thread, which
may help interruptible tasks.
Otherwise, you should simply make sure
to check isCancelled() regularly in
your doInBackground() method. You can
see examples of this at
code.google.com/p/shelves.

Categories

Resources