call finish() from static method

﹥>﹥吖頭↗ 提交于 2019-12-04 23:46:25

Create a reference to your activity in onCreate with

//onCreate
final Activity activity = this;

Then you can use that in your onCompleted callback

activity.finish();

You might have to make Activity activity global.

EDIT 2/26/2014:

Note that calling finish() from a static method is probably bad practice. You are telling a specific instance of an Activity with it's own lifecycle that it should shut itself down from a static method, something without any lifecycle or state. Ideally you'd call finish() from something with a binding to the Activity.

For some, bclymer's method may not work. It didn't on mine, using the latest beta version Android Studio... Try this...

public class myActivity extends Activity {

    public static Activity activity = null;
    ...
    ...

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.myActivity_layout);

        activity = this;
        ....
        ....
    }
}

from your other activity within the same package, simply ....

    // use try catch to avoid errors/warning that may affect the 
    // next method execution
    try {
         myActivity.activity.finish();
    } catch (Exception ignored) {}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!