trying not to repeat myself (android/java)

社会主义新天地 提交于 2020-01-07 05:23:27

问题


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!


回答1:


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 {
    ...
}



回答2:


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.




回答3:


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");


来源:https://stackoverflow.com/questions/24631282/trying-not-to-repeat-myself-android-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!