简体   繁体   中英

How to override method from extend activity?

I have 2 activity. One is AlertDialogActivity & second is MainActivity . I extend AlertDialogActivity in MainActivity .

like this public class MainActivity extends AlertDialogActivity

now how to override method which is in AlertDialogActivity to my MainActivity ?

AlertDialogActivity:

package com.jimmytrivedi.alertdialog;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class AlertDialogActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alert_dialog);
        showAlertDialog(AlertDialogActivity.this);
    }

    private void showAlertDialog(final Context context) {
        // 1. Instantiate an <code><a href="/reference/android/app/AlertDialog.Builder.html">AlertDialog.Builder</a></code> with its constructor
        AlertDialog.Builder builder = new AlertDialog.Builder(AlertDialogActivity.this);

// 2. Chain together various setter methods to set the dialog characteristics
        builder.setMessage("How are you?")
                .setTitle("Hello");

// 3. Get the <code><a href="/reference/android/app/AlertDialog.html">AlertDialog</a></code> from <code><a href="/reference/android/app/AlertDialog.Builder.html#create()">create()</a></code>
        AlertDialog dialog = builder.create();
        dialog.show();


    }

}

Make the showAlertDialog protected instead of private , or even public if you want other classes to be able to call it.

protected void showAlertDialog(final Context context) {

}

Private methods can not be overriden.

if you want to override a method Use abstract keyword

public abstract class AlertDialogActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_alert_dialog);
    showAlertDialog(AlertDialogActivity.this);
}

  public abstract void showAlertDialog(final Context context) {

    // 1. Instantiate an <code><a href="/reference/android/app/AlertDialog.Builder.html">AlertDialog.Builder</a></code> with its constructor
    AlertDialog.Builder builder = new AlertDialog.Builder(AlertDialogActivity.this);

  // 2. Chain together various setter methods to set the dialog characteristics
    builder.setMessage("How are you?")
            .setTitle("Hello");

   // 3. Get the <code><a 
  href="/reference/android/app/AlertDialog.html">AlertDialog</a></code> from <code><a 
  href="/reference/android/app/AlertDialog.Builder.html#create()">create()</a></code>
    AlertDialog dialog = builder.create();
    dialog.show();


}

Method should be with the same name and type. Method should also not be private .

It can be protected, package-private or public . More about that you can find here: What is the difference between public, protected, package-private and private in Java?

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