1

I have an Activity class.

public class A extends Activity
{
}

Then I have a class that is not an Activity but I want it to start the Activity A.

public class B
{
    public B()
    {
       Intent I = new Intent(null, A.class);
       i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    }
}

The code is take from this question which should work: Calling startActivity() from outside of an Activity? However, when I run it I never change from my first activity to activity A. What am I doing wrong? Should I be listening to the FLAG_ACTIVITY_NEW_TASK inside A?

Community
  • 1
  • 1
Mike John
  • 818
  • 4
  • 11
  • 29

1 Answers1

4

Something like this should work:

public class B {
Context context;

public B(Context context) {
    this.context = context;

}

public void startNewActivity(String str) {
    try {
        Intent i = new Intent(context, Class.forName(str));
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
}
}

Use case of class B:

    B b = new B(A.this);
    b.startNewActivity("MainActivity");//the "MainActivity" is the className of the java class

Note I find this way wierd and a overkill.

Illegal Argument
  • 10,090
  • 2
  • 44
  • 61
  • I might be able to use it if this line `A.class` can be something more generic that would work for other activities. Maybe using `context.class` (that would probably fail.) – Mike John Sep 05 '14 at 03:08