-3

I am getting user information from a database using asynctask because you cannot do that in its a front task... when the task is completed i want to get back to the point where i called this Asynctask is there any way of doing this ?

my goal is to start another activity when the asynctask is completed

package com.example.eightmiles;
 import android.app.AlertDialog;  
 import android.content.Context;  
 import android.os.AsyncTask;
import android.print.PrintAttributes;
import android.widget.Toast;  
 import java.io.BufferedReader;  
 import java.io.BufferedWriter;  
 import java.io.IOException;  
 import java.io.InputStream;  
 import java.io.InputStreamReader;  
 import java.io.OutputStream;  
 import java.io.OutputStreamWriter;  
 import java.net.HttpURLConnection;  
 import java.net.MalformedURLException;  
 import java.net.URL;  
 import java.net.URLEncoder;
import java.util.StringTokenizer;  

 public class Backgroundtask extends AsyncTask<String, Void, String> {  
  AlertDialog alertDialog;  
  AlertDialog alertDialog2;  
   Context ctx;  
   Signin signin;
   Userlocalstore userlocalstore;
   String res_name , res_username , res_dob , res_email , res_id , res_pass;
   Backgroundtask(Context ctx)  
   {  
    this.ctx =ctx;  
   }  

   @Override  
   protected void onPreExecute() {  
   alertDialog = new AlertDialog.Builder(ctx).create();  
     alertDialog.setTitle("Login Information....");  
     alertDialog2 = new AlertDialog.Builder(ctx).create();  

     alertDialog2.setTitle("Apologies");  
   }  

   @Override  
   protected String doInBackground(String... params) {  
     String reg_url = "http://8miles.freeiz.com/Register.php";  
     String login_url = "http://8miles.freeiz.com/getuserinfo.php";  
     String method = params[0];  
     if (method.equals("Register")) {  
       String name = params[1];  
       String user_name = params[2];  
       String user_pass = params[3];  
       String email = params[4];  
       String dob = params[5];  
Daim Shahzad
  • 65
  • 1
  • 5

3 Answers3

1

In the OnPostExecute method of the AsyncTask, start the new activity via an Intent and pass the loaded data along to it.

@Override
protected void onPostExecute(String data) {
     Intent intent = new Intent(MainActivity.this, WhateverActivityToStart.class);
     intent.putExtra("SOME_DATA", data);
     MainActivity.this.startActivity(intent);
 }

You'll need to get familiar with starting an activity and passing data between activities

Community
  • 1
  • 1
Boots
  • 181
  • 1
  • 6
0

That is what onPostExecute() is for. You can check the docs and see the fourth step.

Basically, you won't be able to return to the point where you called the AsyncTask because that's what an asynchronous process does by design.

Instead, since onPostExecute() gets called in the UI thread when the AsyncTask is finished you can use that to start an activity, show a toast and other things.

Acapulco
  • 3,373
  • 8
  • 38
  • 51
  • thats the point... i am unable to start an activity using onPostExecute().. – Daim Shahzad Jul 22 '15 at 00:10
  • on "Intent intent = new Intent(this, WhateverActivity.class);" it gives me an error : "he constructor Intent(Backgroundtask, Class) is undefined" and on this line "startActivity(finallogin);" it gives me an error : "The method startActivity(Intent) is undefined for the type Backgroundtask" – Daim Shahzad Jul 22 '15 at 00:11
  • Nothing on your questions indicates that you were aware of onPostExecute. Also, you could post that error then so we can actually work out what's going wrong. – Acapulco Jul 22 '15 at 00:41
0

I prefer to use callbacks when trying to do something in an AsyncTask, and then going back to do work in my Activity... although an answer has been accepted here's a nice bit of code:

public class Backgroundtask extends AsyncTask<String, Void, String> { 
    private final TaskCallback taskCallback;
    private final Context ctx;
    ...
    ...

    public interface TaskCallback{
        public void taskComplete(String data);
    }

    Backgroundtask(Context ctx, TaskCallback taskCallback){  
        this.ctx = ctx;
        this.taskCallback = callback
    }

     ...
     ...

    //truncating the rest of your code, adding just the extra bit needed

    @Override
    protected void onPostExecute(String data) {
        super.onPostExecute(data);
        taskCallback(data);
    }
}

Then in your activity you should have something like:

 public class MyActivity extends Activity implements TaskCallback{

     //again, truncating most of what would be here like onCreate, etc...

    @Override
    public void taskComplete(String data){
        //call more work here if needed

        Intent intent = new Intent(MyActivity.this, WhateverActivityToStart.class);
        intent.putExtra("SOME_DATA", data);
        startActivity(intent);

        finish(); //May or may not need this, don't want them coming back probably
    }

 }
Lucas Crawford
  • 3,078
  • 2
  • 14
  • 25