In my app, I navigate between about 5 different screens, each in its own activity
. Pretty much any activity
can be called from any other activity
, so I am trying to build a helper file to manage the intents
so that I don’t have redundant code.
I built a helper file with public static
methods and pass the activity context
and any required data when calling these methods. This appears to be working fine on my device (Samsung Galaxy S5), but Android Studio recommends making my intents AtomicReference in my helper file.
Can you help me understand if and why these should be AtomicReference<Intent>
?
Also, is it appropriate to pass context
to a helper file to make these calls?
ActivityHelper file:
public class ActivityHelper {
private ActivityHelper() {}
public static void startAddNewMealActivity(Context context) {
Intent newMealIntent = new Intent(context, MealEditActivity.class);
context.startActivity(newMealIntent);
}
public static void startMealListActivity(Context context) {
Intent intent = new Intent(context, MealListActivity.class);
context.startActivity(intent);
}
public static void startEditMealActivity(Context context, FBMeal meal, String mealFBKey) {
Intent intent = new Intent(context, MealEditActivity.class);
intent.putExtra(Constants.INTENT_FB_KEY_EXTRA_TAG, mealFBKey);
intent.putExtra(Constants.INTENT_MEAL_EXTRA_TAG, meal);
context.startActivity(intent);
}
public static void startEditLastMealActivity(final Context context) {
FBHelper.getQueryForMostRecentMeal().addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (FBHelper.isExistingDataSnapshop(dataSnapshot)) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
FBMeal selectedMeal = snapshot.getValue(FBMeal.class);
String selectedMealId = snapshot.getKey();
startEditMealActivity(context, selectedMeal, selectedMealId);
}
} else {
Utils.showToastFromStringResource(R.string.no_meals, context);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
Utils.showToastFromStringResource(R.string.error_getting_meal, context);
}
});
}
}
Example of calling helper file from menu in AppCompatActivity:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.edit_meal_menu:
ActivityHelper.startEditMealActivity(this, meal, mealFBKey);
return true;
case R.id.edit_last_entry_menu:
ActivityHelper.startEditLastMealActivity(this);
return true;
case R.id.about_menu:
DialogFragment newFragment = AboutDialog.newInstance();
newFragment.show(getFragmentManager(), "about");
default:
return super.onOptionsItemSelected(item);
}
}
I cannot see any reason at all why you would need to use an AtomicReference
in any of your static
methods.
Another approach would be to create a BaseActivity
class that extends AppCompatActivity
and includes all of your helper methods. All of your activities should then extend BaseActivity
. In that case, you would not need to pass a Context
to all of these helper methods, since the helper methods would be non-static and can just use this
as Context
.