Commits

Spencer Herzberg committed 2cd5327

implementing a simple ContextSingleton and injecting it to the main activity

Comments (0)

Files changed (2)

src/main/java/com/example/HomeActivity.java

     @Inject
     private Context appContext;
 
+    @Inject
+    private SimpleCalculator simpleCalculator;
+
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         toaster.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View view) {
-                Toast toast = Toast.makeText(appContext, "This is a toast br0!", Toast.LENGTH_LONG);
-                toast.show();
+                Toast.makeText(appContext, "This is a toast br0!", Toast.LENGTH_LONG).show();
+            }
+        });
+
+        toaster.setOnLongClickListener(new View.OnLongClickListener() {
+            @Override
+            public boolean onLongClick(View view) {
+                Toast.makeText(appContext, "4+4=" + simpleCalculator.add(4, 4), Toast.LENGTH_LONG).show();
+                return false;
             }
         });
     }

src/main/java/com/example/SimpleCalculator.java

+package com.example;
+
+import android.app.Activity;
+import com.google.inject.Inject;
+import roboguice.inject.ContextSingleton;
+
+@ContextSingleton
+public class SimpleCalculator {
+
+    @Inject
+    private Activity activity;
+
+    public int add(int x, int y) {
+        return x + y;
+    }
+
+    public int subtract(int x, int y) {
+        return x - y;
+    }
+
+}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.