我正在寻找从不是Activity的类启动intent的最佳方法,但它是Activity类的包含对象。
例如活动类:
Class MainActivity extends ListActivty
{
...
TestLauncher tester;
}
以及我想从中开始意图的课程:
Class TestLauncher
{
public TestLauncher ()
{
//Code to create an intent needs a Context
//Intent i = new Intent(Context, class)
//Code to start activity needs to be called with an Activity
//Activity.StartActivity(i);
}
}
在架构上这样做的最佳方法是什么?我应该将MainActivity
作为参数传递给TestLauncher
的构造函数吗?或者有更好的方式我不知道吗?
答案 0 :(得分:4)
Class TestLauncher
{
public TestLauncher (Context c)
{
Intent i = new Intent(c, YourActivity.class)
c.startActivity(i);
}
}
TestLauncher ts=new TestLauncher(getApplicationContext());