Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The class doesn't need to extend Activity, just pass the Context of the calling Activity to the printMessage method by adding an extra parameter like this...</p> <pre><code>public void printMessage(Context context, String message, int time) { Toast toastMessage = Toast.makeText(context, "", Toast.LENGTH_SHORT); ... } </code></pre> <p><strong>EDIT:</strong> In response to your comment...</p> <p>From what I can tell, you've created a 'helper' class which isn't Android specific but needs to use Android elements (such as the Context of Activities, SharedPreferences etc). In many cases these things can be passed into the various methods of the helper class (without problem) in the way I demonstrated above.</p> <blockquote> <p>It does work, but, unfortunately now I get too many ToastMessages.</p> </blockquote> <p>OK, so obviously it's being called repeatedly before the previous Toast times out. Try making 'toastMessage' an instance member of your helper class...</p> <pre><code>public class MyHelper { Toast toastMessage = null; public void printMessage(Context context, String message, int time) { if (toastMessage != null) toastMessage.cancel(); toastMessage = Toast.makeText(context, "", Toast.LENGTH_SHORT); ... } } </code></pre> <blockquote> <p>Unfortunately, getSharedPreferences() does need Activity, or at least the way I'm calling it.</p> </blockquote> <p>Again, any methods of your 'helper' class can be passed the <code>Context</code> of any Activity which calls them and this can be used for getting preferences etc.</p> <p>Alternatively, as mentioned by FvZ you can extend the Application class and provide access to the application-wide Shared Preferences...</p> <pre><code>public class MyApp extends Application { protected static SharedPreferences UserPrefs = null; @Override public void onCreate() { super.onCreate(); UserPrefs = getSharedPreferences("MyAppPrefs", MODE_PRIVATE); } } </code></pre> <p>All Activities can then simply use (for example)...</p> <pre><code>MyApp.UserPrefs.putString(...); </code></pre> <p>In this case, you could also create your helper class as a static member of your Application making it accessible from all Activities.</p> <blockquote> <p>Is there a simpler way to use methods similar to regular Java?</p> </blockquote> <p>One thing to grasp is that Android does use 'regular' Java...it's just that something like the Android Activity class is a very special case. When one Activity covers another, you have no guarantee how long the first Activity will stay alive. In many cases it will be destroyed very quickly - for that reason, you can't rely on being able to call a method in one Activity from another. It's also not the way that Android is supposed to work.</p>
 

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