Static call for startActivity from AsyncTask - android

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

Related

How to post http after oncreate complete?

I have a MainActivity that has onCreate method in it. In that class, it should load the layout and post data to server. As long as the activity started, it becomes blank and only go to the next activity SignUpActivity. what I need is the MainActivity layout will show first and then execute the httpPost.
Here are my code
public class MainActivity extends Activity {
private Context context;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar = (ProgressBar) findViewById(R.id.progress_bar);
context = this;
new MainAsyncTask().execute(""); // where should i put this code? here??
}
private class MainAsyncTask extends AsyncTask<Object, Integer, String> {
#Override
protected String doInBackground(Object... params) {
String result = null;
String url = "some url";
Log.i("Post", "Post HTTP");
HttpPostHelper.postData(url); // method to post the http. i made myself
intent = new Intent(context, SignUpActivity.class);
startActivity(intent);
return result;
}
}
}
i just want that the MainAsyncTask.execute("") is executed after the layout is fully shown on the device.
SOLVED
I move the new MainAsyncTask().execute(""); and add it here, and it works fine:
#Override
protected void onStart() {
super.onStart();
new MainAsyncTask().execute("");
}
Try in onStart() method of activity
calling on AsynTask seems ok but you shouldn't call your intent in doInBackground()
call it in onPostExecute() method
protected void onPostExecute(String result){
intent = new Intent(context, SignUpActivity.class);
startActivity(intent);
}
You can try a Thread.sleep before startActivity to give the main layout some time to load and show. You should also call the startActivity in onPostExecute.
as per your code it should be like this. Take care of # doinbackground() as here it is not correct.
public class MainActivity extends Activity {
private Context context;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar = (ProgressBar) findViewById(R.id.progress_bar);
context = this;
new MainAsyncTask().execute(""); // where should i put this code? here??
}
private class MainAsyncTask extends AsyncTask<Object, Integer, String> {
protected void onPreExecute( ){
//start Progressbar
progressBar.show();
}
#Override
protected String doInBackground(Object... params) {
String result = null;
String url = "some url";
Log.i("Post", "Post HTTP");
HttpPostHelper.postData(url); // method to post the http. i made myself
return result;
}
protected void onPostExecute(String result){
//stuff u want in main thread
progressBar.cancell();
intent = new Intent(context, SignUpActivity.class);
startActivity(intent);
}
}
}

Context is not available in Fragment of another Activity

I have an Activity with DrawerLayout. I am inflating an Fragment in FrameLayout of this Activity xml file. Xml file contain the GridView inside the LinearLayout.
I apply setOnItemClickListener on this GridView inside the fragment onActivityCreated method.
But on this method I am calling AsyncTask Class of my Activity.
So I am not getting the Context when I call this AsyncTask from my setOnItemClickListener.
Please give me some suggestion how to do it or any alternative if possible.
My Activity
class GetExamList extends AsyncTask<String, String, String>{
Context context;
public GetExamList(Context mContext){
context=mContext;
}
#Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(context);
pDialog.setMessage("Fetching Test List");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
#Override
protected String doInBackground(String... params) {
exam_id=1;
List<NameValuePair> list = new ArrayList<NameValuePair>();
list.add(new BasicNameValuePair("exam_id", exam_id+""));
JSONObject json = jsonParser.makeHttpRequest(url_exam_list,
"POST", list);
// check for success tag
try {
int success = json.getInt("flag");
Log.d("flag", success+"");
if (success == 1) {
//fetch exam list
JSONArray elist=json.getJSONArray("testdata");
Log.d("flag=1", "in try of HomeExamList");
for (int i = 0; i < elist.length(); i++) {
JSONObject obj=elist.getJSONObject(i);
int testId=obj.getInt("test_id");
String testName=obj.getString("test_name");
Log.d("elist test_id", testId+"");
Log.d("elist test_name", testName);
Test test=new Test();
test.setExam_id(exam_id);
test.setTest_id(testId);
test.setTest_name(testName);
Dao dao=new Dao(context);
dao.open();
boolean check=dao.chechTestIdInTestList(testId);
if(!check) dao.insertTestList(test);
dao.close();
}
} else {
// failed to create product
Toast.makeText(getApplicationContext(), "unsuccessful", 2000).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
pDialog.dismiss();
Intent intent = new Intent(context, ExamList.class);
intent.putExtra("eid", exam_id);
startActivity(intent);
}
}
You need to use getActivity() is alternative of context in fragment base design.
getActivity() Return theActivity` this fragment is currently associated with.
Android docs:
http://developer.android.com/reference/android/app/Fragment.html#getActivity%28%29
((MainActivity) getActivity()) is castable to your Activity.
There are ways to get the context:
If you are in an Activity:
this;//will call your activity's context
getApplicationContext();//will get the whole application context
If you are in a Fragment:
getActivity();//will call the activity context
If you are getting the context inside an inner class ex. Asynctask in a Fragment:
Fragment_class_name.this.getActivity();//explicitly get the reference of your fragment and call the context
If you are getting the context inside an inner class ex. Asynctask in a Activity:
Activity_class_name.this;//explicitly get the reference of your activity context
Edit:
change this:
startActivity(intent);
to this:
context.startActivity(intent);
create private Context mContext;
mContext=getActivity();
now you can use mContext instead of getActivity();

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.
}
}
}

Android: How to update an UI from AsyncTask if AsyncTask is in a separate class?

I hate inner class.
I've a main activity who launches a 'short-life' AsyncTask.
AsyncTask is in a separate file, is not an inner class of main activity
I need async task updates a textView from main Activity.
I know i can update a TextView from onProgressUpdate, if AsyncTask is a inner class
But how from an external, indipendent, async task ?
UPDATE: This looks like working :
In acitivty i call the task
backgroundTask = new BackgroundTask(this);
backgroundTask.execute();
In the constructor i've
public BackgroundTask(Activity myContext)
{
debug = (TextView) myContext.findViewById(R.id.debugText);
}
where debug was a private field of AsyncTask.
So onProgressUpdate I can
debug.append(text);
Thanks for all of you suggestions
AsyncTask is always separate class from Activity, but I suspect you mean it is in different file than your activity class file, so you cannot benefit from being activity's inner class. Simply pass Activity context as argument to your Async Task (i.e. to its constructor)
class MyAsyncTask extends AsyncTask<URL, Integer, Long> {
WeakReference<Activity> mWeakActivity;
public MyAsyncTask(Activity activity) {
mWeakActivity = new WeakReference<Activity>(activity);
}
...
and use when you need it (remember to NOT use in during doInBackground()) i.e. so when you would normally call
int id = findViewById(...)
in AsyncTask you call i.e.
Activity activity = mWeakActivity.get();
if (activity != null) {
int id = activity.findViewById(...);
}
Note that our Activity can be gone while doInBackground() is in progress (so the reference returned can become null), but by using WeakReference we do not prevent GC from collecting it (and leaking memory) and as Activity is gone, it's usually pointless to even try to update it state (still, depending on your logic you may want to do something like changing internal state or update DB, but touching UI must be skipped).
Using Interface
1) Create one Interface
public interface OnDataSendToActivity {
public void sendData(String str);
}
2) Implements it in your Activity
public class MainActivity extends Activity implements OnDataSendToActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
new AsyncTest(this).execute(new String[]{"AnyData"}); // start your task
}
#Override
public void sendData(String str) {
// TODO Auto-generated method stub
}
}
3) Create constructor in AsyncTask(Activity activity){}
Register your Interface in AsyncTask file
and call interface method as below.
public class AsyncTest extends AsyncTask<String, Integer, String> {
OnDataSendToActivity dataSendToActivity;
public AsyncTest(Activity activity){
dataSendToActivity = (OnDataSendToActivity)activity;
}
#Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
dataSendToActivity.sendData(result);
}
}
Here, your OnPostExecute will call after all task done by AsyncTask and will get "result"
as a parameter, returned by doInBackground(){ return "";}.
While "dataSendToActivity.sendData(result);" it will call activity's overrided method "public void sendData(String str) {}".
An edge case to remember: Be sure to pass this, i.e. you current activity's context to AsyncTask and not create another instance of your activity, otherwise your Activity will be destroyed and new one is created.
Make an static function in your activity class passing context in it to update your text view and then call this function in your AsynkTask class to update.
In Activity class:
public static void updateTextView(){
//your code here
}
In AynckTask class call this function.
Just pass the context (activity or whatever) to your AsyncTask in a constructor and then in onSuccess or onProgressUpdate call whatever you need on the context.
I wrote a small extension to AsyncTask for this kind of scenario. It allows you to keep your AsyncTask in a separate class, but also gives you convenient access to the Tasks's completion:
public abstract class ListenableAsyncTask<Params, Progress, Result> extends AsyncTask<Params, Progress, Result>{
#Override
protected final void onPostExecute(Result result) {
notifyListenerOnPostExecute(result);
}
private AsyncTaskListener<Result> mListener;
public interface AsyncTaskListener<Result>{
public void onPostExecute(Result result);
}
public void listenWith(AsyncTaskListener<Result> l){
mListener = l;
}
private void notifyListenerOnPostExecute(Result result){
if(mListener != null)
mListener.onPostExecute(result);
}
}
So first you extend ListenableAsyncTask instead of AsyncTask. Then in your UI code, make a concrete instance and set listenWith(...).
The Question has already been answered, still im posting how it should be done i guess..
Mainactivity class
public class MainActivity extends Activity implements OnClickListener
{
TextView Ctemp;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Ctemp = (TextView) findViewById(R.id.Ctemp);
doConv = (Button) findViewById(R.id.doConv);
doConv.setOnClickListener(this);
}
#Override
public void onClick(View arg0) // The conversion to do
{
new asyncConvert(this).execute();
}
}
now in the async class
public class asyncConvert extends AsyncTask<Void, Void, String>
{
SoapPrimitive response = null;
Context context;
public asyncConvert(Context callerclass)
{
contextGUI = callerclass;
}
.
.
.
.
protected void onPostExecute(String result)
{
((MainActivity) contextGUI).Ctemp.setText(result); // changing TextView
}
}
/**
* Background Async Task to Load all product by making HTTP Request
* */
public static class updateTExtviewAsyncTask extends AsyncTask<String, String, String> {
Context context;
ProgressDialog pDialog;
String id, name;
String state_id;
//--- Constructor for getting network id from asking method
public updateTExtviewAsyncTask(Context context,String id,String city)
{
context = context;
state_id = id;
city_name = city;
}
/* *
* Before starting background thread Show Progress Dialog
* */
#Override
protected void onPreExecute()
{
super.onPreExecute();
pDialog = ProgressDialog.show(context, "","Please wait...", true, true);
pDialog.show();
}
/**
* getting All products from url
* */
protected String doInBackground(String... args)
{
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
YourClass.UpdateTextViewData("Textview data");
}
}
// place this code inside your activity class and also declare updating textview static
public static void UpdateTextViewData(String tvData)
{
tv.setText(tvData);
}

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();
}
}

Categories

Resources