Jay
Jay

Reputation: 634

How to get Activity to inherit methods

I have a dozen activities that will inherit the same methods for a pin pad. Each activity is totally different except for the pin pad. How can I get my activity to inherit the methods from a common class? Am I doing something wrong?

public class TestActivity extends PinPadActivity {
    TextView mEntry;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        mEntry = (TextView) findViewById(R.id.entry_textview);
    }
}


public class PinPadActivity extends AppCompatActivity {
    String mPin;
    String mPinShow;
    TextView mEntry;
    public void on1Click(View v) {
        mPin = mPin+"1";
        mPinShow = mPinShow+"*";
        mEntry.setText(mPinShow);
    }
    public void on2Click(View v) {
        mPin = mPin+"2";
        mPinShow = mPinShow+"*";
        mEntry.setText(mPinShow);
    }
    public void on3Click(View v) {
        mPin = mPin+"3";
        mPinShow = mPinShow+"*";
        mEntry.setText(mPinShow);
    }
    public void on4Click(View v) {
        mPin = mPin+"4";
        mPinShow = mPinShow+"*";
        mEntry.setText(mPinShow);
    }
    public void on5Click(View v) {
        mPin = mPin+"5";
        mPinShow = mPinShow+"*";
        mEntry.setText(mPinShow);
    }
    public void on6Click(View v) {
        mPin = mPin+"6";
        mPinShow = mPinShow+"*";
        mEntry.setText(mPinShow);
    }
    public void on7Click(View v) {
        mPin = mPin+"7";
        mPinShow = mPinShow+"*";
        mEntry.setText(mPinShow);
    }
    public void on8Click(View v) {
        mPin = mPin+"8";
        mPinShow = mPinShow+"*";
        mEntry.setText(mPinShow);
    }
    public void on9Click(View v) {
        mPin = mPin+"9";
        mPinShow = mPinShow+"*";
        mEntry.setText(mPinShow);
    }
    public void on0Click(View v) {
        mPin = mPin+"0";
        mPinShow = mPinShow+"*";
        mEntry.setText(mPinShow);
    }
    public void onClearClick(View v) {
        mPin = "";
        mPinShow = "";
        mEntry.setText("");
    }
}

Upvotes: 0

Views: 81

Answers (2)

CodePlay
CodePlay

Reputation: 2203

Irrespective the way of coding or whether it is best approach, in order to make your code works, you should remove the TextView mEntry in your TestActivity and set public or protected modifier to the TextView mEntry in the PinPadActivity (default or private variable is unable to be accessed by subclasses). This should allow you to call the onXClick() methods to set the mEntry textview inherited from the PinPadActivity in your TestActivity.

Upvotes: 1

Gabe Sechan
Gabe Sechan

Reputation: 93561

That can be made to work, but its not the right way. Make the PinPad into a Fragment. Then include that Fragment in each Activity that uses it.

Upvotes: 0

Related Questions