whats up?
i'm a beginner to development and everywhere i read 'do not repeat yourself', so i really mind about repeating myself.
there is a piece of code that i copied and pasted in every single activity of my app, and i suspect there might be a way of not repeating it - maybe creating a class and calling its methods - but i don't know how to implement it.
the code below is what i'm talking about: something i use in my navigation drawer and it's inside the main class of each activity of my app:
@Override
public void onItemClick(AdapterView<?> parent, View arg1, int position,
long arg3) {
if (position == 0) {
Toast.makeText(this, "categories baby", Toast.LENGTH_SHORT).show();
} else if (position == 1){
final Context context = this;
Intent getUserData = getIntent();
String userEmail = getUserData.getStringExtra("user_email");
Intent intent = new Intent(context, NewAdActivity.class);
intent.putExtra("userEmail", userEmail);
startActivity(intent);
} else if (position == 2){
final Context context = this;
Intent getUserData = getIntent();
String userEmail = getUserData.getStringExtra("user_email");
Intent intent = new Intent(context, AdListActivity.class);
intent.putExtra("userEmail", userEmail);
startActivity(intent);
} else {
Session session = Session.getActiveSession();
session.closeAndClearTokenInformation();
Intent intent = new Intent(context, MainActivity.class);
startActivity(intent);
}
}
can you give me some hints on how to reuse this rather than copy/paste?
thanks!
You can create an new class that extends Activity
and implements your code and then make all your activities extend that class.
That code seems to be the implementation of the AdapterView.OnItemClickListener
, so you can create something like:
public abstract class MyActivityWithListener extends Activity
implements AdapterView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View arg1, int position, long arg3) {
....
}
}
And then your main activity could be something like:
public class MainActivity extends MyActivityWithListener {
...
}
As you can clearly see in the following block
} else if (position == 1){
final Context context = this;
Intent getUserData = getIntent();
String userEmail = getUserData.getStringExtra("user_email");
Intent intent = new Intent(context, NewAdActivity.class);
intent.putExtra("userEmail", userEmail);
startActivity(intent);
} else if (position == 2){
final Context context = this;
Intent getUserData = getIntent();
String userEmail = getUserData.getStringExtra("user_email");
Intent intent = new Intent(context, AdListActivity.class);
intent.putExtra("userEmail", userEmail);
startActivity(intent);
}
You are executing the same thing twice, except for the class of the activity. Therefore, you can rip it out into a function like so,
} else if (position == 1){
beginAdActivity(NewAdActivity.class);
} else if (position == 2){
beginAdActivity(AdListActivity.class);
}
}
public void beginAdActivity(Class<?> activityClass)
{
final Context context = this;
Intent getUserData = getIntent();
String userEmail = getUserData.getStringExtra("user_email");
Intent intent = new Intent(context, activityClass);
intent.putExtra("userEmail", userEmail);
startActivity(intent);
}
And ta-dah, it was written down only once.
Just create an util class like this
public class MyUtils{
public static void jumpToNewAd(Activity mContext){
Intent getUserData = mContext.getIntent();
String userEmail = getUserData.getStringExtra("user_email");
Intent intent = new Intent(mContext, NewAdActivity.class);
intent.putExtra("userEmail", userEmail);
mContext.startActivity(intent);
}
public static void jumpToAdList(Activity mContext){
Intent getUserData = mContext.getIntent();
String userEmail = getUserData.getStringExtra("user_email");
Intent intent = new Intent(mContext, AdListActivity.class);
intent.putExtra("userEmail", userEmail);
mContext.startActivity(intent);
}
public static void jumpToMain(Activity mContext){
Session session = Session.getActiveSession();
session.closeAndClearTokenInformation();
Intent intent = new Intent(mContext, MainActivity.class);
mContext.startActivity(intent);
}
public static void ting(Activity mContext,String message){
Toast.makeText(mContext, message, Toast.LENGTH_SHORT).show();
}
}
Call it wherever you want
MyUtils.jumpToAdList(this);
MyUtils.jumpToNewAd(this);
MyUtils.jumpToMain(this);
MyUtils.ting(this,"Catagories Baby");