Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between "public void onDestroy()" and "protected void onDestroy()"?

Here is a part of my code:

package com.admobsdk_dfp_handler;

import com.google.ads.*;
import com.google.ads.doubleclick.*;

import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.view.Menu;
import android.widget.RelativeLayout;

public class AdMobSDK_DFP_Handler extends Activity {
    private DfpAdView adView;
    private static final String adUnitId = "/7732/test_portal7/android_app1_test_portal7/top_banner_non_sdk_application_android_app1_test_portal7";
    private Handler handler = new Handler();
    private Runnable runnable = new Runnable() {

        public void run() {
            adView.loadAd(new AdRequest());
            handler.postDelayed(this, 5000);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ad_mob_sdk__dfp__handler);

        adView = new DfpAdView(this, AdSize.BANNER, adUnitId);

        RelativeLayout layout = (RelativeLayout) findViewById(R.id.mainLayout);

        layout.addView(adView);

        adView.loadAd(new AdRequest());

    };

    @Override
    protected void onPause() {
        handler.removeCallbacks(runnable);
        super.onPause();
    }

    @Override
    protected void onResume() {
        handler.postDelayed(runnable, 5000);
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        handler.removeCallbacks(runnable);
        super.onDestroy();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_ad_mob_sdk__dfp__handler,
                menu);
        return true;
    }

}

protected void onDestroy() is used to clean up the program. However, I saw some sample projects, which usually use public void onDestroy() to do the clean up.

As I know that in Java, protected method can be accessed within a package, and public method can be accessed anywhere. But usually a class has its own onDestroy(), so is it mean that either one can be used?? Any points I have to concern about while choosing between them?

Thank you for your help.

like image 567
Kit Ng Avatar asked Dec 04 '12 19:12

Kit Ng


2 Answers

Between public and protected there's no difference in the functionality. The only difference is that with public you will be able to call onDestroy() on an instance of the activity from any class. With protected you can call only in the same class or subclasses in the same package. With onDestroy() I don't see a lot of sense in making it public. It could be actually bad practice.

In other words, just use protected unless you have a very special reason to make it accessible to more classes.

I think the public implementations you saw is just a mistake / not necessary. If you want to test just change it back to protected. If there are no compiler errors, it's not necessary.

like image 103
User Avatar answered Sep 22 '22 05:09

User


The definition of onDestroy in Activity is protected onDestroy(). In your subclass of Activity, you can write protected onDestroy or public onDestroy and the compiler will not complain. The more conventionally accepted programming practice would be to write protected onDestroy, minimising the exposure of the method. However, Writing public onDestroy is highly unlikely ever to cause a bug. So people put it in without thinking and forget about it. At least, that's what I do.

like image 39
emrys57 Avatar answered Sep 18 '22 05:09

emrys57