1. Spencer Herzberg
  2. android-maven-helloworld

Commits

Spencer Herzberg  committed 8712bd2

injecting an android Context

  • Participants
  • Parent commits 3b288d5
  • Branches master

Comments (0)

Files changed (1)

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

View file
 import android.view.View;
 import android.widget.Button;
 import android.widget.Toast;
+import com.google.inject.Inject;
 import roboguice.activity.RoboActivity;
 import roboguice.inject.ContentView;
 import roboguice.inject.InjectView;
     @InjectView(R.id.toast)
     private Button toaster;
 
+    @Inject
+    private Context appContext;
+
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         toaster.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View view) {
-                Context context = getApplicationContext();
-                Toast toast = Toast.makeText(context, "This is a toast br0!", Toast.LENGTH_LONG);
+                Toast toast = Toast.makeText(appContext, "This is a toast br0!", Toast.LENGTH_LONG);
                 toast.show();
             }
         });