How can I get the current android activity?

0

I'm doing some tests of android apps in eclipse, and I have a method to click the back arrow to which I pass a Activity :

public void flechaAtras(Activity activity) {
    Display mdisp = activity.getWindowManager().getDefaultDisplay();
    Point mdispSize = new Point();
    mdisp.getSize(mdispSize);
    int maxX = mdispSize.x; 
    int maxY = mdispSize.y;

    TouchAction touchAction = new TouchAction(getDriver());
    touchAction.tap(new PointOption().withCoordinates (250, maxY - 80)).perform();
}

How can I get the activity that I send?

    
asked by Juan 22.10.2018 в 16:20
source

2 answers

0

Simply with a .this after the name of your class you can do it

NombreDeTuActivity.this 
    
answered by 22.10.2018 в 16:34
0

To get the Activity current which would be sent to the method flechaAtras(...) you can do it in different ways:

If you are in an Activity, use this :

flechaAtras(this);

If you are in an Activity, but in turn in a listener, you must use the name of the class MyActivity.this :

flechaAtras(MyActivity.this);

If you are in a Fragment, use getActivity() :

flechaAtras(getActivity());

If you are in a class which does not extend from Activity or Fragment , it is necessary to send the constructor of this class the value of Actvity :

flechaAtras(valorActivity);
    
answered by 22.10.2018 в 19:41