1

Good day, this is my QuestionActivity. Class and I want ArrayList<BasicItem> BasicList to put this on different class and still use it in this activity.

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_question);


    final ArrayList<BasicItem> BasicList = new ArrayList<>();
    BasicList.add(new BasicItem();
    BasicList.add(new BasicItem();
    BasicList.add(new BasicItem();
    BasicList.add(new BasicItem();

My reason to separate this because, in the actual app, this activity become laggy and I'm gonna put 600 items. and I think separating it might help. Thanks in advance.

Zain
  • 37,492
  • 7
  • 60
  • 84
  • Do you mean you're making `BasicList` a static field somewhere instead of a local variable? (Side note: `basicList` is a better name, uppercase variables are confusing) – user May 10 '20 at 23:48

1 Answers1

0

You just need to create a separate utility class that can be sharable for different activities/fragments), and you can utilize the singleton pattern for this to avoid multiple instantiations.

public class Utility {

    ArrayList<BasicItem> BasicList;
    private static Utility mUtility;

    private Utility() {
        BasicList = new ArrayList<>();
        BasicList.add(new BasicItem());
        BasicList.add(new BasicItem());
        BasicList.add(new BasicItem());
        BasicList.add(new BasicItem());
    }

    static Utility newInstance() {
        if (mUtility == null) {
            mUtility = new Utility();
        }
        return mUtility;
    }

}

And to use it in activity/fragment

Utility utility = Utility.newInstance();
BasicItem item = utility.BasicList.get(0);

You can also consider using the .

Zain
  • 37,492
  • 7
  • 60
  • 84
  • Thanks a lot. How abt this one sir ? ```final Pattern b1a1 = Pattern.compile("(?=.*a)(?=.*b)(?=.*c)");``` ```final Pattern b1a2 = Pattern.compile("(?=.*d)(?=.*e)(?=.*f)");``` ```final Pattern[] b1 = new Pattern[]{b1a1, b1a2};``` – John Kenneth Custodio May 11 '20 at 14:20
  • @JohnKennethCustodio this is a regex; can you clarify more what you need to achieve – Zain May 11 '20 at 15:57
  • that code is for this ```if (b1[int].matcher(answer1.getText().toString()).find())``` and like the array list, i will put hundreds of that thing. I am using that regex in substitute of contains() (because contains() doesnt work with me well) . All I need is put that in different java class (Utility) and still access it in my Main java class.. Btw, separating codes like that reduce lag of the activity in actual game ? if not, I will not separate it – John Kenneth Custodio May 11 '20 at 19:25
  • @JohnKennethCustodio separating code is will give you more control on parts of your app, will ease maintenance in the future and make it more testable so you can test each part separately. you know it's a bad idea to have a GOD class that have too many lines of irrelevant codes – Zain May 11 '20 at 22:20