Storing decentralized Android stuff

This may not make much sense from the point of view of the Android SDK, but in C ++ I use the main.cpp function (and especially main ()) as the place where I declare and initialize other classes / objects, and subsequently everything that my application really takes place in these classes. I will never go back and check anything in main.cpp afterwards. But in the Java, Android SDK, you have to override dozens of methods in the main activity, and all this happens in one file. Example:

I have MainActivity.java and SomeTest.java in my project, where the first class is MainActivity by default, which extends Activity, and SomeTest.java contains a class that declares and starts a new Thread. I initialize the SomeTest class from MainActivity.java and pass it an activity descriptor as a parameter:

SomeTest test = new SomeTest(MainActivity.this); 

And with the MainActivity handle, I continue to do all of this newly created thread. When I need to update the user interface, I use runOnUiThread () to create and display a new ListView (for example) on my main layout. I want to get the width and height of the newly created Listview, for which I have to override onWindowFocusChanged () in my MainActivity.java and notify the stream from there, since getWidth () and getHeight () will only have values โ€‹โ€‹when the ListView is actually displayed on the screen. For me it is not a good practice to make such connections ("callbacks", if you want) from MainActivity to this thread.

Is there a way to save methods such as onWindowFocusChanged () in a stream, and not touch MainActivity.java at all?

As I said, this may not make sense.

+7
source share
4 answers

Is there a way to save methods such as onWindowFocusChanged () in a stream, and not touch MainActivity.java at all?

onWindowFocusChanged() - callback method. It is called activity. You cannot change this.

And with the MainActivity handle, I continue to do all of this newly created thread.

This is not a good idea at all. Using a background thread, say, to load some data from a file or database is quite reasonable (although using Loader or AsyncTask might be better). However, as a rule, the background thread should not know or care about such things as the "width and height of the newly created ListView".

Of course, you can transfer some logic from your activity to other classes. You can use certain frameworks for this, such as fragments or custom views. However, the class structure should not be driven by your threading model. For example, return to your opening statement:

in C ++ I use to maintain my main.cpp (and especially the main () function) as the place where I declare and initialize other classes / objects and then everything that my application does in these classes

However, in C ++ you would not say that you are blocked only ever having two classes, one of which works in some background thread. Although you may have a class or classes that use a background thread (or threads), the driving force behind the class structure is not โ€œI have a background thread,โ€ but โ€œI want to reuse XYZ logicโ€ or โ€œI want to use the class hierarchy to support the template strategies "or some such.

+4
source

Personally speaking, the Context idea taken from the Android SDK seems dirty. What you are describing is due to too much responsibility for the Activity . This is why you need to track many things inside a single file ( Activity life cycle, getting an instance of Context to show Dialog , etc.). I do not think that there is an ideal solution, but I would recommend using:

  • Fragment subclasses that help divide the screen (and so on into logic) into separate parts.
  • third-party structures / libraries such as AndroidAnnotations , RoboGuice , Otto , which are great tools to avoid spaghetti code.
+1
source

if you want to do some UI updates from another class, consider using AsyncTask , passing it the Views you need to update. Let me know if you need an example.

0
source

I read everything and understand your statements, I see that you have ever done programming, but apparently just starting with Android, I already did a lot to build systems, so I completely understood the concept of software availability it looks like this:

 void run(){ object.setup(); while(true){ otherObject.run(); } } 

But there is one fundamental flaw in your logic of your question:

Android programming is another paradigm of programming from C ++ and computer programming, and you should understand its specific paradigm instead of assuming what is good practice from other paradigms.

Quote from you: create and show a new ListView (for example) on my main layout. I want to get the width and height of the newly created Listview, for what I have to override onWindowFocusChanged() create and show a new ListView (for example) on my main layout. I want to get the width and height of the newly created Listview, for what I have to override onWindowFocusChanged() .

From this, I see that you are really trying to make Android stuff in a way that is not recommended in the context of Android. You can easily implement ListView from the XML layout of setContentView(int) and use Activity onCreate to instantiate any stream infrastructure (AsyncTaskLoader) to load data in the background and deliver it back to the user interface.

This does not mean that all your code will be dumped in one file, which makes it useless. This small example that I will tell you can be done with an Activity that implements loader callbacks, a separate class with a loader, a separate class with data loading work, a separate class with a data adapter, activity is just the central part that organizes and manage these classes at the right moment in the life cycle and in no case need to call onWindowFocusChanged() and have well-organized code.

Also, please refer to CommonsWare, as it is usually expertly written and fixed.

0
source

All Articles