Alia Ramli Ramli
Alia Ramli Ramli

Reputation: 425

call another activity in class that did not extend Activity class

Hi all i have a class name

public class WikipediaDataSource extends NetworkDataSource{.....}

which extend to NetworkDataSource. what im try to do is from this class i would like to call new activity...

Intent i = new Intent(context, Obj3DView.class);
startActivity(i);

i got error saying

the method of startActivity(intent) is undefined for the type WikipediaDataSource 

i read a lot on this issue .. it happens because this class do not extend the activity clas.

i try to follow others solutions. but it does not work for my case.

Please help! :)

Upvotes: 1

Views: 1067

Answers (2)

Shmulik Almani
Shmulik Almani

Reputation: 1

In the non activity class:

private Activity activity;

public non_activity_class(Activity act) {

        activity=act;

}
.....`enter code here`

and then when you want to start a new activity

Intent in = new Intent("Pakege.NewActivity");
// NewActivity = what you write in your Manifest            

activity.startActivity(in);

In the activity class, call the constructor

non_activity_class(this);

I hope it helps.

Upvotes: 0

Sam
Sam

Reputation: 86958

If this line works:

Intent i = new Intent(context, Obj3DView.class);

Then you already have access to a Context, just use:

context.startActivity(i);

Since startActivity() is a method of the Context class.


actually the line intent i = new Intent(context, Obj3DView.class); got error.

Often developers pass the Context in the constructor:

public class WikipediaDataSource extends NetworkDataSource {
    Context context;

    public WikipediaDataSource (Context ctx) {
        ...
        context = ctx;
    }
    ...
}

Now your code should work.

Upvotes: 8

Related Questions