1

I want to ask a question about optimizing my code. I am working on a project and i have code working for onItemClick listner and button click handler the problem is that i have 9 different activities and i have to copy and paste the same code in all my activities the issue i am having is too much of same code copy pasted into each activity enter image description here

The Highlighted Section of the sliding menu are same in all activies all i have to do is register their click listners again and again to make them working and copy the same code in all activities. i want it to be generic i.e. code written in one place should be working for all the activities. This app is in final launch mode and i cannot shift to sliding menu using navigation drawer that was the main reason i used this approach and the top menu also has different buttons which clicks need to be managed dynamically. i tried making this static but it didnt worked. Thanking You for your time and replies.

Umer Kiani
  • 3,783
  • 5
  • 36
  • 63

4 Answers4

3

That sounds like the perfect use for a fragment. Place the views and the related code in a fragment, and include the fragment in each activity.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • Agreed but this app has been made using activities and is at the end of launch it makes nearly impossible to shift this to navigation drawer that is what the main issue is. – Umer Kiani Jun 02 '14 at 05:53
2

What Gabe mentions would be the perfect way to go. However, if you do want to continue with multiple activities, you could create a class extending Activity with all the code for the sliding menu inside it. Then make sure that all other activities extend the new class you created.

Shivam Verma
  • 7,973
  • 3
  • 26
  • 34
1

visit Android Sliding Menu using Navigation Drawer tutorial for using navigation drawer...

this may help you..

Pragnesh Ghoda シ
  • 8,318
  • 3
  • 25
  • 40
0

This is the Solution to this Problem

public class SuperActivity extends Activity implements OnClickListener,
    OnItemClickListener {

protected static Button btn_logout;
protected static ListView lv_SlidingMenu;
protected static FlyOutContainer rootView;
protected static TextView tv_userName;
protected static TextView tv_memberSince;
protected static ImageView iv_userImage;
protected static ImageView iv_top_home;
protected static TextView tv_top_home;
protected ImageView iv_slidingmenu;
protected static SlidingMenuAdapter slidingMenuAdapter = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
        long id) {
    // TODO Auto-generated method stub
    switch(parent.getId()){

    case R.id.list:

        switch(position){
        case 0:
            GeneralDataModel.actionIntent = new Intent(this,
                    ActivityTheGreatControversy.class);
            this.startActivity(GeneralDataModel.actionIntent);
            break;

        case 1:
            break;

        case 2:

            GeneralDataModel.actionIntent = new Intent(this,
                    AtlastActivity.class);
            this.startActivity(GeneralDataModel.actionIntent);
            break;

        case 3:

            GeneralDataModel.actionIntent = new Intent(this,
                    MediaActivity.class);
            this.startActivity(GeneralDataModel.actionIntent);

            break;

        case 4:

            GeneralDataModel.actionIntent = new Intent(this,
                    TimeLineActivity.class);
            this.startActivity(GeneralDataModel.actionIntent);

            break;

        case 6:

            GeneralDataModel.actionIntent = new Intent(getApplicationContext(),
                    ActivityNotes.class);
            this.startActivity(GeneralDataModel.actionIntent);
            GeneralDataModel.actionIntent = null;
            break;

        case 10:
            GeneralDataModel.actionIntent = new Intent(this,
                    ActivitySettings.class);
            this.startActivity(GeneralDataModel.actionIntent);
            rootView.toggleMenu();
            break;

        default:
            rootView.toggleMenu();
            break;

        }


        break;
    }

}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    Toast.makeText(getApplicationContext(), "CLICKEDiy", Toast.LENGTH_LONG)
            .show();
    switch (v.getId()) {

    case R.id.btn_sliding_logout:
        GeneralDataModel.actionIntent = new Intent(getApplicationContext(),
                LoginSignup.class);
        GeneralDataModel.actionIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(GeneralDataModel.actionIntent);
        GeneralDataModel.actionIntent = null;
        new SessionManager(this).logoutUser();
        this.finish();
        break;


    case R.id.iv_home_slidingmenu:
        rootView.toggleMenu();
        break;

    }

}

protected void fillSlidingMenu() {

    tv_userName.setText(UserInformation.getFirstName() + " "
            + UserInformation.getLastName());
    tv_memberSince.setText(UserInformation.getMemberSince());

    lv_SlidingMenu.setAdapter(slidingMenuAdapter);
}
}

and derive your all activities from This Class

Then in their onClickListners just simply call super.onclick(v);

Umer Kiani
  • 3,783
  • 5
  • 36
  • 63