46

I was surfing in Android code because I wanted to see what is into Activity.finish() method.

I just wanted to have the confirmation that in Activity.finish() there would be a call to onDestroy() method.

But what I found in this method (and in many others) was:

public void finish() {
    throw new RuntimeException("Stub!");
}

So WHERE Can I find the code that really destroys the Activity? Thanks!

Sneh Pandya
  • 8,197
  • 7
  • 35
  • 50
Alessandro Argentieri
  • 2,901
  • 3
  • 32
  • 62
  • Possible duplicate of [SQLiteDatabase getWritableDatabase() { throw new RuntimeException("Stub!"); }](https://stackoverflow.com/questions/27651148/sqlitedatabase-getwritabledatabase-throw-new-runtimeexceptionstub) – Josh Lee Nov 22 '17 at 20:10

3 Answers3

50

This is because source code is not found in SDK. To see the source code, you need to download source for Android SDK, so Android studio can display the respective code. See this image

Mahdi-Malv
  • 16,677
  • 10
  • 70
  • 117
hakim
  • 3,819
  • 3
  • 21
  • 25
  • 1
    This is true, but note that it will not download any Native classes – Tim Oct 07 '16 at 09:25
  • Very strange because I have in SDK Manager>Appearance & Behavior>System Settings >Android SDK >SDK Platforms > Android 7.0 (Nougat)> Sources for Android 24 (INSTALLED)... But when I visit the class Activity in the upper part of the editor there is this message: Sources for 'Android API 24 Platform' not found. I click on Download, but nothing happens! :-O – Alessandro Argentieri Oct 07 '16 at 09:42
  • 2
    @AlexMawashi please, restart your android studio, based on my experience, sometimes it work after AS restarted – hakim Oct 07 '16 at 09:54
  • Nothing. It reports: "Decompiled .class file, bytecode versione: 52.0 (Java 8)" and "Sources for 'Android API 24 Platform' not found". The file Activity opened is at this path: C:/Users/alessandro.argentier/AppData/Local/Android/sdk/platforms/android-24/android.jar!/android/app/Activity.class – Alessandro Argentieri Oct 07 '16 at 13:53
  • @AlexMawashi you need to update your Java SDK to java 8, or you use target sdk 23, and than download the Source for Android SDK API level 23 – hakim Oct 07 '16 at 14:52
  • Can this source be downloaded without Android Studio? Etc., as Maven dependency? – zygimantus Jan 03 '18 at 14:35
  • That is a correct answer in fact android studio when I open the inbuilt file it shows me an option to download that sources. So no doubt your answer is correct, Thanks:) – beginner Aug 05 '21 at 07:22
3

I don't know where you looked, but the code for finish() is this

/**
 * Call this when your activity is done and should be closed.  The
 * ActivityResult is propagated back to whoever launched you via
 * onActivityResult().
 */
public void finish() {
    finish(DONT_FINISH_TASK_WITH_ACTIVITY);
}

which calls the private implementation

/**
 * Finishes the current activity and specifies whether to remove the task associated with this
 * activity.
 */
private void finish(int finishTask) {
    if (mParent == null) {
        int resultCode;
        Intent resultData;
        synchronized (this) {
            resultCode = mResultCode;
            resultData = mResultData;
        }
        if (false) Log.v(TAG, "Finishing self: token=" + mToken);
        try {
            if (resultData != null) {
                resultData.prepareToLeaveProcess(this);
            }
            if (ActivityManagerNative.getDefault()
                    .finishActivity(mToken, resultCode, resultData, finishTask)) {
                mFinished = true;
            }
        } catch (RemoteException e) {
            // Empty
        }
    } else {
        mParent.finishFromChild(this);
    }
}

Important here is ActivityManagerNative.getDefault().finishActivity which you can find at line 3359 in this file https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/app/ActivityManagerNative.java

If you want to dive deeper, you can just follow the trail.

Tim
  • 41,901
  • 18
  • 127
  • 145
3

You are checking in .class not .java file.

Muneesh
  • 433
  • 10
  • 19
  • The class file doesn't contain source code like `public void finish() { throw new RuntimeException("Stub!"); }` Does it?? – LarsH Jul 30 '19 at 14:53
  • check Activity.class, there is: protected void onDestroy() { throw new RuntimeException("Stub!"); } – Muneesh Aug 01 '19 at 07:46
  • 3
    My point is that .class files don't contain source code at all. They contain compiled bytecode. If the OP is looked at the decompilation of a .class file, then it must reflect the source code in a .java file somewhere, right? In which case, the question is why the .java file contains this stub code. – LarsH Aug 02 '19 at 14:00