3

I get an error

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

It has something to do with only running on devices which are api 17 or higher, is there anyway around this?

 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(); } 
1
  • What is your Manifest.xml configuration? It seems you are running code on unsupported versions. Commented Jul 20, 2015 at 15:22

1 Answer 1

2

This API was added only in API Level 17 Check this!

To avoid the crash, you can verify with the following code

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

To get it working on 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 extend this BaseActivity as follows

public class MainActivity extends BaseActivity { ... } 

EDIT (After OP added code snippets)

First create a BaseActivity.java file as I have shown above. Then, make all your activities extend BaseActivity instead of Activity.

Now, change

private WeakReference<Activity> mActivityRef; 

to

private WeakReference<BaseActivity> mActivityRef; 

And change

Activity activity = mActivityRef.get(); 

to

BaseActivity activity = mActivityRef.get(); 
Sign up to request clarification or add additional context in comments.

2 Comments

How would I implement that in this though, I attached a code snippet
What is the activity object here referring to? Can you paste the part of the code where you assign the activity object.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.