简体   繁体   English

在静态方法中使用getActivity()

[英]Making use of getActivity() in a static method

public class QRProductActivityPageObject {


    public static void testProductDetail() throws InterruptedException {

        ActivityTestRule<ProductInfoActivity> rule = new ActivityTestRule<ProductInfoActivity>(ProductInfoActivity.class, true, false);
        Intent intent = new Intent();
        intent.setClass(getActivity(), ProductInfoActivity.class);
        rule.launchActivity(intent);
        Thread.sleep(2000);
     }
}

The above syntax show error that you cant access getActivity() in static method . 上面的语法显示了您无法以静态方法访问getActivity()的错误。 But i want to call this new Activity(ProductInfoActivity) through static method only.Any suggestion?? 但是我只想通过静态方法来调用这个新的Activity(ProductInfoActivity)。任何建议吗?

Pass context when method call in Activity or fragment and get Activity from context in static method 在Activity或fragment中调用方法时传递上下文,并在静态方法中从上下文获取Activity

 public static void testProductDetail(Context context) throws InterruptedException {

ActivityTestRule<ProductInfoActivity> rule = new  ActivityTestRule<ProductInfoActivity>(ProductInfoActivity.class, true, false);
Intent intent = new Intent();
Activity activity = (Activity) context;
intent.setClass(activity , ProductInfoActivity.class);
rule.launchActivity(intent);
Thread.sleep(2000);
 }

Call static method In activity 在活动中调用静态方法

  testProductDetail(getApplicationContext());

In fragment 片段中

  testProductDetail(getActivity().getApplicationContext());

You can get the activity from view: 您可以从视图中获取活动:

Activity activity = (Activity)view.getContext()

If you use FragmentActivity (it seems to be so), then cast Context to FragmentActivity (instead of regular Activity) and further you will able to call getSupportFragmentManager() 如果您使用FragmentActivity(似乎是这样),则将Context强制转换为FragmentActivity(而不是常规的Activity),然后您将能够调用getSupportFragmentManager()

FragmentActivity activity = (FragmentActivity)view.getContext();
FragmentManager manager = activity.getSupportFragmentManager();

Hope this works for you. 希望这对您有用。

Or use the solution I found around here a few weeks ago, it's pretty awesome and elegant. 或者使用几周前在这里找到的解决方案,它非常棒且优雅。 You'd be able to use it anywhere in your APP! 您将可以在APP的任何位置使用它!

  1. Create a class MyApplication (you could name it differently): 创建一个类MyApplication(您可以使用其他名称):

      public class MyApplication extends Application { private static Context context; public void onCreate() { super.onCreate(); MyApplication.context = getApplicationContext(); } public static Context getAppContext() { return MyApplication.context; } } 
  2. Go to your app Manifest file and add to the <application... tag the android:name attribute with the value of the name of the class we created: 转到您的应用清单文件,然后将android:name属性添加到<application...标签中,并添加我们创建的类的名称:

     <application android:allowBackup="true" android:icon="@drawable/appico" android:label="@string/app_name" android:name="MyApplication" 

Now you can get the context for your app from any class like that 现在,您可以从类似的任何类中获取应用程序的上下文

MyApplication.getAppContext(); MyApplication.getAppContext();

You don't need to call getActivity(). 您不需要调用getActivity()。 Instead of using setClass() method which requires you to give a context object, you can use setClassName(String packageName, String className) method. 可以使用setClassName(String packageName,String className)方法来代替使用需要提供上下文对象的setClass()方法。 Try this: 尝试这个:

Intent intent = new Intent();
intent.setClassName("package.name.of.your.app", 
        ProductInfoActivity.class.getName());

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

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