【问题标题】:Android check if this activity is not null/destroyedAndroid 检查此活动是否不为空/已销毁
【发布时间】:2015-06-12 16:39:07
【问题描述】:

Stackoverflow 中有很多关于使用 getActivity()==null 检查片段中的活动是否为空的问题已得到解答

如何检查活动本身的活动是否不为空?

我的具体情况是这样的:

activity启动一个asynctask,然后activity被销毁,然后asynctask在onPostExecute中返回,它调用activity中的一个方法(它被注册为该任务的监听器)并且这个方法使用对THIS的引用来传递一个上下文一个方法。但是,上下文为空。

编辑:这是一些代码。

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() 而不是这个,也许我根本不会有问题?

【问题讨论】:

  • 你为什么使用 asynctask 而不是 asynctaskloader? stackoverflow.com/questions/7120813/…
  • 这是一个不同的问题。我正在修复别人的代码,现在无法重写他们的应用程序 :)
  • 发布一些示例代码,说明您要描述的内容以及问题所在。
  • 这永远不会为空。错误不是内容为空,而是文件目录为空。 toString 是在归档目录而不是上下文上调用的。
  • 我不认为你的活动是空的,因为你正在泄漏它。实际的 null 是 context.getExternalFilesDir(null) 的结果

标签: android


【解决方案1】:

我不确定您的 Activity 为何被销毁。尽管您可以使用 Bundle 重新创建 Activity。 Google 关于活动的documentation 提供了以下示例,用于保存和恢复您的活动实例。

以下内容将保存您的 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();
}

【讨论】:

  • 我已经用代码和建议编辑了我的答案。我不想恢复活动状态,如果活动死了我只想什么都不做。
  • 更新了我的答案。试一试!
【解决方案2】:

使用myActivity.isDestroyed()

reference to doc

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-23
    • 1970-01-01
    • 1970-01-01
    • 2012-01-14
    • 1970-01-01
    • 2014-08-10
    • 2014-06-19
    相关资源
    最近更新 更多