Android如何检查当前活动(activity)是否为空或已销毁?

3
在Stackoverflow上有很多关于从片段检查活动是否为null的已回答问题,使用getActivity()==null 如何在活动本身中检查活动是否不为null?
我的具体情况是:
活动启动一个异步任务,然后销毁活动,然后asynctask在onPostExecute中返回,它调用活动中的一个方法(该方法作为该任务的侦听器注册),并且此方法使用对THIS的引用来传递方法中的上下文。但是上下文为null。
编辑:这里是一些代码。
public interface OnGetStuffFromServerListener {

    void onGetStuffSuccess();

}

public class SomeActivity implements OnGetStuffFromServerListener {

    @Override
    public whatever onCreate() {
        new GetStuffFromServer(this).execute();
    }

    @Override
    public void onGetStuffFromServerSuccess() {
        deleteSomeFiles(this); // NPE -> How do I check if activity still exists here?
    }

    private void deleteSomeFiles(Context context) {
        ... 
        context.getExternalFilesDir(null).toString(); // NPE toString on a null object reference
    }

}

public class GetSomeStuffFromServer extends AsyncTask<Void, Void, Void> {

    private OnGetSomeStuffFromServerListener listener;

    public GetSomeStuffFromServer (OnGetSomeStuffFromServerListener listener) {
        this.listener = listener;
    }

    ...doInBackground

    onPostExecute() {

        if(listener!=null) {
            listener.onGetSomeStuffFromServerSuccess();
        }

    }


}

实际上,如果我使用getApplicationContext()而不是this,也许就不会有任何问题了?


你为什么要使用AsyncTask而不是AsyncTaskLoader?https://dev59.com/pWw05IYBdhLWcg3wpDRu - Andrea De Gaetano
1
那是另一个问题。我正在修复别人的代码,现在不能重写他们的应用程序 :) - Kaloyan Roussev
发布一些示例代码,说明您正在尝试描述的问题所在。 - cyroxis
3
这将永远不会为空。错误并不是内容为空,而是文件目录为空。toString被调用的是filed directory而不是context。 - cyroxis
谢谢大家,我会记住的。 - Kaloyan Roussev
显示剩余2条评论
2个回答

1
我不确定为什么你的Activity会被销毁。虽然你可以使用Bundle重新创建Activity。Google关于Activities的文档提供了以下保存和恢复Activity实例的示例。
以下内容将保存Activity的状态:
static final String STATE_SCORE = "playerScore";
static final String STATE_LEVEL = "playerLevel";
...

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    // Save the user's current game state
    savedInstanceState.putInt(STATE_SCORE, mCurrentScore);
    savedInstanceState.putInt(STATE_LEVEL, mCurrentLevel);

    // Always call the superclass so it can save the view hierarchy state
    super.onSaveInstanceState(savedInstanceState);
}

以下内容将被称为恢复您的 Activity 的先前状态。请注意,逻辑包含在 onCreate() 中,因此您似乎需要再次初始化您的 Activity。
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); // Always call the superclass first

    // Check whether we're recreating a previously destroyed instance
    if (savedInstanceState != null) {
        // Restore value of members from saved state
        mCurrentScore = savedInstanceState.getInt(STATE_SCORE);
        mCurrentLevel = savedInstanceState.getInt(STATE_LEVEL);
    } else {
        // Probably initialize members with default values for a new instance
    }
    ...
}

如果有帮助,请告诉我!

编辑:

尝试在onDestroy()中取消操作。如果Activity已经调用了onDestroy(),那么它的内存已经被设备释放。确保您没有在代码的其他地方处置Activity。

@Override
    protected void onDestroy() {
    asynctask.cancel(true);
    super.onDestroy();
}

我已经编辑了我的答案,并附上了代码和建议。我不想恢复活动状态,如果活动已经结束,我只想什么都不做。 - Kaloyan Roussev
更新了我的答案。试一试吧! - Zack Matthews

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接