简体   繁体   中英

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 . But i want to call this new Activity(ProductInfoActivity) through static method only.Any suggestion??

Pass context when method call in Activity or fragment and get Activity from context in static method

 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 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!

  1. Create a class MyApplication (you could name it differently):

      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:

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

You don't need to call getActivity(). Instead of using setClass() method which requires you to give a context object, you can use setClassName(String packageName, String className) method. Try this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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