简体   繁体   中英

Android check if this activity is not null/destroyed

There are a lot of answered questions in Stackoverflow about checking if activity is null from a fragment, using getActivity()==null

How do I check if activity is not null in the activity itself?

My specific case is this:

activity starts an asynctask, then activity is destroyed, then asynctask returns in onPostExecute, it invokes a method in the activity (which is registered as a listener for that task) and this method uses a reference to THIS to pass a context in a method. The context is null, though.

EDIT: Here is some code.

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

    }


}

Actually, if I am using getApplicationContext() instead of this, maybe I will not have a problem at all?

I'm not sure why your Activity is being destroyed. Although you may be able to recreate the Activity using a Bundle. Google's documentation on Activities gives the following sample for saving and restoring an instance of your activity.

The following will save the state of your 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);
}

The following will be called to restore your Activity's previous state. Note that the logic is contained in onCreate(), so it sounds like you will have initialize your Activity again.

@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
    }
    ...
}

Let me know if that helps!

edit:

Try canceling the operation in onDestroy(). If the Activity has called onDestroy() its memory has been released by the device. Make sure you aren't disposing of your Activity anywhere else in your code.

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

Use myActivity.isDestroyed()

reference to doc

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