Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
421 views
in Technique[技术] by (71.8m points)

android - 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...