Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Start a new Activity from non Activity class

I want to start a new activity in non-Activity class that implements a DialogListener following is my code:

public class FacebookLoginDialog implements DialogListener {
  @Override
  public void onComplete(Bundle values) {
    HomeActivity.showInLog(values.toString());

    Intent i1 = new Intent (this, SearchActivity.class);
    startActivity(i1);
  }

  @Override
  public void onFacebookError(FacebookError e) {
    // TODO Auto-generated method stub
  }

  @Override
  public void onError(DialogError e) {
    // TODO Auto-generated method stub
  }

  @Override
  public void onCancel() {
    // TODO Auto-generated method stub
  }
}

I can't start the new activity using intent in onComplete method, please help.

Thanks

like image 797
Nadeem Avatar asked Sep 30 '12 19:09

Nadeem


Video Answer


2 Answers

This doesn't work because you need a Context in order to start a new activity. You can reorganize your class into something like this:

public class FacebookLoginDialog implements DialogListener {
  private final Context context;

  public FacebookLoginDialog(Context context) {
    this.context = context;
  }

  @Override
  public void onComplete(Bundle values) {
    HomeActivity.showInLog(values.toString());

    Intent i1 = new Intent (context, SearchActivity.class);
    context.startActivity(i1);
  }

  //Other methods...
}

Then it will work.

like image 66
Malcolm Avatar answered Oct 01 '22 02:10

Malcolm


Pass context as constructor parameter and then try this

Intent i = new Intent(this, SearchActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
like image 20
Peter Moskala Avatar answered Oct 01 '22 03:10

Peter Moskala