Skip to content Skip to sidebar Skip to footer

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

Solution 1:

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:

publicclassFacebookLoginDialogimplementsDialogListener {
  privatefinal Context context;

  publicFacebookLoginDialog(Context context) {
    this.context = context;
  }

  @OverridepublicvoidonComplete(Bundle values) {
    HomeActivity.showInLog(values.toString());

    Intenti1=newIntent (context, SearchActivity.class);
    context.startActivity(i1);
  }

  //Other methods...
}

Then it will work.

Solution 2:

Pass context as constructor parameter and then try this

Intent i = newIntent(this, SearchActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);

Solution 3:

use starActivity from non-activity class:

Intentintent=newIntent(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"));

Solution 4:

For Easy Usage you can a method for this particular method:

publicclassSomething
{
   publicstaticvoidnavigate(Context context,  Class<?> nameOfClass)
    {
        Intenti=newIntent(context, nameOfClass);
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);
    }
}

can be called in other class and method everytime by calling this:

Something.navigate(activityName.this, classYourWantTONavigateTo.class);

Post a Comment for "Start A New Activity From Non Activity Class"