Java error api level method

I am getting the error

java.lang.NoSuchMethodError: android.app.Activity.isDestroyed

Is this because it only works on devices that are api 17 or higher anyway?

       private WeakReference<Activity> mActivityRef;

    @Override
        public void onFinish() {

            Activity activity = mActivityRef.get();
//            if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
//do your thing!

                if (activity != null && !activity.isFinishing() && !activity.isDestroyed()) {
                    activity.startActivity(new Intent(activity, ListOfAlarms.class));
                    activity.finish();
                }
                mStarted = false;

//            }
//            Intent goBack = new Intent(CountDownAct.this, ListOfAlarms.class);
//            startActivity(goBack);
//            finish();
        }

      

+3


source to share


1 answer


This API was only added in API Level 17 Check it out!

To avoid crashing, you can check with the following code

if(Build.VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR1){
//do your thing!
}

      

To make it work in lower API levels, you can create your own BaseActivity.java and add code like this

public class BaseActivity extends Activity {
    private boolean mDestroyed;

    public boolean isDestroyed(){
        return mDestroyed;
    }

    @Override
    protected void onDestroy() {
        mDestroyed = true;
        super.onDestroy();
    }
}

      

Now make all your activities an extension of this BaseActivity like this

public class MainActivity extends BaseActivity {
...
}

      

Hope this helps! Accept this answer if it works for you :)

EDIT (after adding the OP's code snippets)



First, create the file BaseActivity.java

as I showed above. Then do all your steps BaseActivity

instead Activity

.

Now change

private WeakReference<Activity> mActivityRef;

      

to

private WeakReference<BaseActivity> mActivityRef;

      

And change

Activity activity = mActivityRef.get();

      

to

BaseActivity activity = mActivityRef.get();

      

+2


source







All Articles