user7372142
user7372142

Reputation: 31

Cannot pass values to an AsyncTask in Android

I am using an AsyncTask in an activity.

here is my code

public class MyActivity extends AppCompatActivity {


 EditText editUserNameLogin;
 EditText editPassLogin;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.login);
    //ButterKnife.bind(this);

    editUserNameLogin = (EditText) findViewById(R.id.input_username_login);
    editPassLogin = (EditText) findViewById(R.id.input_password_login);

}

public class AsyncTaskClass extends AsyncTask<String, String, String> {


    String strUserName = editUserNameLogin.getText().toString();
    String passLogin = editPassLogin.getText().toString();


    @Override
    protected void onPreExecute() {

    }

    @Override
    protected String doInBackground(String... params) {

        Toast.makeText(MyActivity.this, passLogin, Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onPostExecute(String r) {

    }
}

but in doInBackground can't get values passLogin or strUserName (Toast.makeText(MyActivity.this, passLogin,) don't show any text)

Upvotes: 0

Views: 152

Answers (7)

IntelliJ Amiya
IntelliJ Amiya

Reputation: 75788

AsyncTask enables proper and easy use of the UI thread. This class allows you to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.

new AsyncTaskClass("SEND STRING").execute();

You can pass this Your Value this way

private class AsyncTaskClass extends AsyncTask<String, String, String> {

    String strRESPONSE="";

    public MyAsyncTask(String str_GET) {
         this.strRESPONSE=str_GET; // print "SEND STRING"

    }


}

Upvotes: 0

Rahul Mandaliya
Rahul Mandaliya

Reputation: 738

You can not perform UI operation inside background AsyncTask doInBackground method because AsyncTask not work with current UI thread, its create new thread while you initialize and execute.

Let me explain you in bref.

While activity start its stay with Activity Thread and when you complete activity operation and destroy its completely remove from operation task. But while you start AsyncTask on Activity its start with individual operation stat that not depends on activity that you start, so if you perform UI operation in doInBackground method and in case Activity destroyed and you working on UI that already destroyed by activity and UI cannot get reference, its generate an exception. So it's necessary to work with current activity thread not another background thread.

There are many case that you can pass data inside AsyncTask, i'm comfortable with below operation, it can help you also.

// Pass data to AsyncTask comma separated values
new MyBackgroundTask().execute("Hello there!","How are you?");

private class MyBackgroundTask extends AsyncTask<String, Void, Void> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected Void doInBackground(String... strings) {
        String message1 = strings[0];
        String message2 = strings[1];

        Log.d("_TAG_", "First String: " + message1);
        Log.d("_TAG_", "Second String: " + message2);
        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
    }
}

For more information read Android Official Documents AsyncTask Developer Guides

Upvotes: 0

Poovarasan Selvaraj
Poovarasan Selvaraj

Reputation: 169

Try this one, inside onCreate

String response="checking";
new AsyncTaskClass().execute(response);

then create inner class AsyncTaskClass,

 private class AsyncTaskClass extends AsyncTask<String,Void,String > {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected String doInBackground(String... strings) {
        String respose1 = strings[0];
        return respose1;
    }

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

Upvotes: 1

fida1989
fida1989

Reputation: 3249

Try the following:

@Override
protected String doInBackground(String... params) {
runOnUiThread(new Runnable() {    
            @Override
            public void run() {
               Toast.makeText(MyActivity.this, passLogin, Toast.LENGTH_SHORT).show();    
            }
    });            
}

Upvotes: 0

Shivam Oberoi
Shivam Oberoi

Reputation: 1445

Try and execute AsyncTaskClass in onCreate

new AsyncTaskClass().execute();  //use this method and call this in onCreate

Upvotes: 1

SpiritCrusher
SpiritCrusher

Reputation: 21043

I have no idea what exactly you want to achieve by such behavior.
But i am pointing out some point here . First of all you can not access any UI element in background thread .

 @Override
protected String doInBackground(String... params) {
    Toast.makeText(MyActivity.this, passLogin, Toast.LENGTH_SHORT).show();
}

The above code is not going to work as doInBackground runs asynchronously separate from UI thread.

If you want to show a toast on AsyncTask started then do it in onPreExecute or after execution do it in onPostExecute.

        @Override
        protected void onPreExecute() {
        Toast.makeText(MyActivity.this, passLogin, Toast.LENGTH_SHORT).show();
        }

And as i see you never execute the AsyncTask then how are you expecting anything from it. Do call execute().

new AsyncTaskClass().execute();

For more on AsyncTask read AsyncTask.

Upvotes: 0

Harsh Patel
Harsh Patel

Reputation: 459

You cant show ui operations like toast in doInBackground if you still want to do that then use this code to display toast while in doInBackground

runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(MainActivity.this, "", Toast.LENGTH_SHORT).show();
            }
        });

and also you need to call yourAsyncTaskObject.execute to start asynctask

Upvotes: 0

Related Questions