Start a new Activity from non Activity class

前端 未结 3 1112
长情又很酷
长情又很酷 2020-12-01 16:15

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

public class FacebookLoginDialog imp         


        
相关标签:
3条回答
  • 2020-12-01 16:37

    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);
    
    0 讨论(0)
  • 2020-12-01 16:43

    use starActivity from non-activity class:

    Intent intent = new Intent(android.content.Intent.ACTION_SEND);
            intent.setType("text/plain");
            intent.putExtra(Intent.EXTRA_TEXT, "YOUR STRING");
            intent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Share via...");
            context.startActivity(Intent.createChooser(intent, "Share"));
    
    0 讨论(0)
  • 2020-12-01 16:52

    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.

    0 讨论(0)
提交回复
热议问题