简体   繁体   English

如何从包含的 Activity 的 class 启动 Intent

[英]How to Start an Intent from a contained class of an Activity

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.我正在寻找从不是活动的 class 开始意图的最佳方式,但它是活动 class 的包含 object。

For example the Activity Class:例如活动 Class:

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

and the class that I want to start the intent from:以及我想从以下位置开始意图的 class :

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?我应该将MainActivity作为参数传递给TestLauncher的构造函数吗? Or is there a better way that I am not aware of?或者有没有更好的方法我不知道?

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

TestLauncher ts=new TestLauncher(getApplicationContext());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM