Where/how exactly do I create an object/class in Android studio?

I have a single project (app) in which I currently have a few functions in a single activity for handling some routines I now need to access elsewhere (from another activity). The functions are static, and I have read that - in Kotlin speak - I need to create an object.

Sounds simple enough... Plenty of examples. But where do I place the object?? At the top of my MainActivity? Since Java/Kotlin is compiled, can I just place it there? Or is there a formal place to create a NEW file dedicated to this object and just include/import it from each activity that needs it?

2 answers

  • answered 2019-07-11 00:16 Rafael Hernández

    There is no consensual way of doing this. It depends on what you need, but you can make your class implement Parcelable, put it in a bundle, the bundle in an intent and use the intent when starting the activity. Something like this:

            Intent intent = new Intent(getApplicationContext(), OtherActivity.class);
            Bundle bundle = new Bundle();
            Parcelable myObject = new MyObject();
            bundle.putParcelable("keyForMyObject", myObject);

  • answered 2019-07-11 03:04 Lee Boon Kong

    Well, it all based on how you want to design and structure your app, for me, I would usually create a new package named objects, or pojo, on my main package and place all my newly created objects there.

    You can do it your way if that object you created are only used by your MainActivity, but if it goes beyond that, creating a new file would be a better choice.