![](/img/trans.png)
[英]How do you make a class varible in an Intent: Intent intent = new Intent(getApplicationContext().this, variable.class);
[英]How to make global “Intent explicitIntent = new Intent(…)” methods
我在各種類(視圖)中有相同的方法,並希望以某種方式抽象它們,所以我在多個類中沒有相同的代碼部分。 我如何在Android中執行此操作?
我在項目中多次使用的方法如下所示:
public void goHome() {
Intent explicitIntent = new Intent(CurrentClass.this, ClassWhereToGo.class);
startActivity(explicitIntent);
}
最后,我只想在整個項目中擁有這個,並能夠從不同的視圖動態訪問它。 我想我必須為goHome(p1,p2)添加兩個參數,以便能夠從各種視圖訪問該方法。 我只是不知道在哪里放置方法以及如何使其與參數一起使用。
我認為理想情況下我會創建一些輔助類?
感謝幫助!
public class Helper
{
public static void goHome(Context context, Class<?> cls) {
Intent explicitIntent = new Intent(context, cls);
context.startActivity(explicitIntent);
}
}
它會像這樣使用:
Helper.goHome(CurrentClass.this, ClassWhereToGo.class);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.