Greetings readers. I have implemented a “very basic” messaging system within one of my applications. An example use case is as follows:
1. User arrives at an activity.
2. The activity realizes that the user needs to be in another activity.
3. The activity posts a user message to the application and sends the user on their way to the other activity.
Here is why I made it for my application: In my Pitching Coach application, the user arrives at the pitch chart when a game is opened. If a pitcher has not yet been created for the game, the pitch chart sees this and sends the user to the create pitcher activity. The pitch chart posts a message similar to “you need to create a pitcher in order to start the game.” to the system and now the create pitcher activity tells the user why they are on that activity. Lets look at how I did this…
I create a custom application class by extending android.app.Application. This class will be live and accessible any time an activity for the app is visible. It’s as simple as creating an object that extends Application and putting the code inside of the root package directory for the app. Inside of the AndroidManifest I edit the application tag to contain the name of the new custom class. This hooks up your application to the new application class. Here is the snippet from the Android Manifest.
The AndroidManifest.xml file
<manifest ... > ... <application android:name="CustomApplication" ... > ... </application> <manifest> |
Inside of the new application class, I create a couple of methods. One for posting a message and one for displaying and deleting the message. I also create a member of type string named applicationMessage. Here is the code:
(Note: Since the work of posting a toast message is done here already, I also add the showToastMessage() method so I can quickly post a toast message to the user at any time. Extra little bonus :))
The CustomApplication.java class
package com.mywebsite.myapplication; import android.app.Activity; import android.app.Application; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import android.widget.Toast; public class CustomApplication extends Application { public CustomApplication() { super(); } /** ----------------------- MESSAGE HANDLING ----------------------- */ private String message = ""; public void setApplicationMessage(String messageText) { message = messageText; } public void showAndDeleteApplicationMessage(Activity activity) { if (message.length() > 0) { LayoutInflater inflater = activity.getLayoutInflater(); View toastLayout = inflater.inflate( R.layout.toast_layout, (ViewGroup) activity.findViewById(R.id.toast_layout_root) ); TextView text = (TextView) toastLayout.findViewById(R.id.toastlayout_text); text.setText(message); message = ""; Toast toast = new Toast(this); toast.setDuration(Toast.LENGTH_LONG); toast.setView(toastLayout); toast.show(); } } public void showToastMessage(Activity activity, Message messageText) { message = messageText; showAndDeleteApplicationMessage(activity); } } |
Now when I want to post a message for an activity I am going to launch. I simply call a reference to my application inside of the first activity and send it a message. Then, in an activity that may be receiving a message, I make a call to showAndDeleteApplicationMessage() inside of onResume() – or onCreate(). If the message string is empty, it does nothing. Here is some brief code on embedding the application class and posting the message:
package com.mymojosport.pitchingcoach; import com.mywebsite.myapplication.R; import com.mywebsite.myapplication.CustomApplication; import android.app.Activity; public class CustomActivity extends Activity { /** ------------------------ INITIALIZATION ------------------------- */ private CustomApplication app; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // create a reference to the custom activity app = (CustomApplication) getApplication(); // show any messages posted to the application app.showAndDeleteApplicationMessage(this); } } |
Now calling the activity and sending it a message is as simple as (from the calling activity):
app.setApplicationMessage("You need to create a pitcher in order to start the game."); Intent intent = new Intent(this, CustomActivity.class); startActivity(intent); |
Hope this helps the world out there!
Randall