Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is fairly out of context code so it's hard to give you a definitive answer but the general principal remains the same: DO NOT REPEAT CODE. You should aim to eliminate all forms of repetition in code, especially when dealing with hardcoded values. Common ways of doing this are to provide static functions in some other class, or create a superclass which contains all the core functionality that you'll repeatedly need. Below is a brief example of how to use <code>inheritance</code> and <code>abstract classes</code> to minimize repetition of code: </p> <p>i.e</p> <pre><code>abstract class CoreMethods extends Activity implements OnClickListener { public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); } /******************************************************************** * SHORTENED TOAST * *********************************************************************/ public void showToast(String value){ Toast.makeText(getApplicationContext(), value, Toast.LENGTH_SHORT).show(); } /******************************************************************** * SHORTENED ALERTDIALOG * *********************************************************************/ public void showAlert(String title, String btn_txt, String message){ final AlertDialog.Builder alert = new AlertDialog.Builder(EventHome.this); alert.setTitle(title).setMessage(message) .setNeutralButton(btn_txt, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { alert.setCancelable(true); }}).show(); } } </code></pre> <p>Save that in a file called CoreMethods.java or something similar. Then, whenever you need those methods you can simply inherit the CoreMethods class and access the functions.</p> <pre><code>public class ExampleClass extends CoreMethods { private void foo() { super.showToast("bar"); super.showAlert("foobar"); } } </code></pre> <p>For more information about inheritance in Java, see <a href="http://www.roseindia.net/java/language/inheritance.shtml" rel="nofollow">here</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload