如何在单独的 class 中为我的所有活动创建明确的意图?

How do I create explicit intents for all my activities in a separate class?

我有一个按钮可以打开一个充满其他活动页面的布局,当我触摸其中任何一个时,它会通过明确的意图跳转到它:

我可以像这样在每个页面上创建多个 onClick 方法:

public void goToPage3(){
    Intent intent = new Intent(Page2.this , Page3.class);
    startActivity(intent);

}
public void goToPage5(){
    Intent intent = new Intent(Page2.this , Page5.class);
    startActivity(intent);

}// and so on up to page20

但是编写代码会花费很多时间,相反我想创建单独的 class,根据需要从每个 activity 创建意图。

public class MenuDetails{

public Intent createIntent(Context from, Class to){
    return new Intent(from,to);
 //Context from is unknown
}

} 问题是我无法弄清楚如何 "know" 从

调用它的当前上下文

你可以这样简单地调用它:

new MenuDetails().createIntent(this, SecondActivity.class);

其中 'this' 是您调用此函数的 activity 的上下文。