Run AsyncTask from another activity

I have a button in my launcher activity which is menu.java and when I click said button I want it to load the other activity MainActivity.java and run the AsyncTask in said actvity without having to click another button.

This is my menu.java activity:

package davidgb.baseballspain;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;

public class Menu extends ActionBarActivity implements View.OnClickListener{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_menu);

    Button beis = (Button) findViewById(R.id.beis);

    beis.setOnClickListener(this);

}

@Override
public void onClick(View view) {
    switch (view.getId()){
        case R.id.beis:
            Intent intent1 = new Intent(this, MainActivity.class);
            startActivity(intent1);
            Intent myIntent = new Intent(this, MainActivity.doit.class);
            startActivity(myIntent);
            //new MainActivity.doit().execute();
            break;

        default:
            break;
    }


}
}

The thing is when i click the button in the menu.java it loads the MainActivity but it doesn't run the AsyncTask directly, it just crashes. Im new to coding, any advice will be highly appreciate.

Edit: Fixed this by creating a new class and implementing the AsyncTask there.

Upvotes: 0

Views: 1448

Answers (4)

Kamran Ahmed
Kamran Ahmed

Reputation: 7761

A lot of things in your code are fundamentally wrong... I'll try to cover the most important parts.

Intent myIntent = new Intent(this, MainActivity.doit.class);
startActivity(myIntent);

The above-mentioned code will assume the subclass doit to be an Activity and try to start it. The crash says there is no declaration of the Activity MainActiviy.doit in your AndroidManfest.xml file.


new MainActivity.doit().execute();

This code will create a new instance of the class MainActivity and execute its doit AsyncTask. This instance will not be the same as the Activity visible to the user, it is a completely different instance.


Handling this case correctly will highly depend on what exactly you are trying to do. If the doit has nothing to do with MainActivity, I would suggest moving it out in a separate class. If it has something to do very specific to MainActivity, you can pass in a few things in your AsyncTask as a parameter like:

new DoIt().execute(lblMyLabel);

Or you can keep it in the MainActivity as it is, but start it based on some Extra as suggested by @jobbert, if the Activity is not started already that is.

According to me, the best way would be to move out doit as a completely different class and pass in a listener (implementation of your own custom interface) and handle UI stuff with that in your MainActivity. Something like:

public class DoSomething {
    public static void doIt(final MyListener listener) {
        new AsyncTask<Void, Void, String>() {
            public String doInBackground(Void... params) {
                return "Something is done here";
            }

            public void onPostExecute(String result) {
                super.onPostExecute(result);

                listener.onDone(result);
            }
        }.execute();
    }

    public interface MyListener {
        public void onDone(String result);
    }
}

And invoke it from your MainActivity or anywhere else like:

DoSomething.doIt(new DoSomething.MyListener() {
    Override
    public void onDone(String result) {
        lblHelloWorld.setText(result);
    } 
});

Upvotes: 1

Salman
Salman

Reputation: 2471

Why you are calling the below

Intent myIntent = new Intent(this, MainActivity.doit.class);
startActivity(myIntent);
break;

It considers that MainActivity.doit.class is also an Activity. While your Activity is MainActivity

Just put the below lines in your button click

Intent intent1 = new Intent(this, MainActivity.class);
startActivity(intent1);

And call your AsyncTask inside the MainActivity onCreate() method like

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        new doitTask().execute();
}

Upvotes: 2

sravs
sravs

Reputation: 330

You have errors in your code.

  1. Class name can't be like MainActivity.doit

  2. There is no break in case statement. you are trying to start MainActivity activity and then MainActivity.doit

  3. MainActivity.doit is not declared in manifest.

So rename MainActivity.doit to someother and declare in manifest and change the case statement to following.

case R.id.beis:
        Intent intent1 = new Intent(this, MainActivity.class);
        startActivity(intent1);
        break;

Upvotes: 1

jobbert
jobbert

Reputation: 3497

I'll try to answer this with the info provided. I think what you're trying to do is not possible or just really bad practice. I'm guessing the AsyncTask of the MainActivity is trying to communicate with the interface of the MainActivity, which it can't. Instead what you should do is start the AsyncTask in the onCreate of the MainActivity, and start it based on bundle extras and it shouldn't crash, or there is something wrong with the AsyncTask that you havn't provided.

Good luck!

Upvotes: 1

Related Questions