How to Start an Intent from a contained class of an Activity

試著忘記壹切 提交于 2020-01-13 19:36:11

问题


I'm looking for the best way to start an intent from a class that is not an Activity, but is a contained object of an Activity class.

For example the Activity Class:

Class MainActivity extends ListActivty
{
...
TestLauncher tester;
}

and the class that I want to start the intent from:

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);
   }
}

What is the best way to do this architecturally? Should I pass MainActivity as a parameter to TestLauncher's constructor? Or is there a better way that I am not aware of?


回答1:


Class TestLauncher
{
   public TestLauncher (Context c)
   {
      Intent i = new Intent(c, YourActivity.class)
      c.startActivity(i);
   }
}

TestLauncher ts=new TestLauncher(getApplicationContext());


来源:https://stackoverflow.com/questions/6337135/how-to-start-an-intent-from-a-contained-class-of-an-activity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!