Hi and thanks for your attention.
I have an Android App (Java) whose code is growing (and eventually will need to grow more and more).
So I have to face the fact that I have make it somehow modular to make it manageable.
So I am here to ask some advice on how to go about it.
Basically the Apps consists of 2 screens, lets' call them A and B.
The App starts with screen A, which is a quiz, if the user responds correctly the screen B is displayed, showing the results; if the user wants to go on and take another quiz the App switches again to screen A, and so on in an infinite loop.
Please can you give me some advice no how to make this app more modular, to avoid all the code written in 'onCreate()'.
In particular how could I implement communication between the modules: how the Quiz module would communicate to the main and ask to switch the screen from A to B; how would the actions performed on screen B would communicate again to the main and ask to switch again to A.
I know it is a very open question, I am looking for some guidance, any suggestion very much appreciated.
For communications between screens, you could use sending messages or better Observer pattern. If you change something on screen B, it call screen A method, that do something.
Generally, for your problem, look at design patterns. Some of them could be usefull and they are widely supported in Java.
I am not very familiar with Android, but iOS has notifications (similar to triggers). Maybe something like that exist also for Android.
Related
I'm creating middleware app for android which in main think will allow people create games based on blocks easily. The idea comes from Siftables (sifteo cubes) - I'm creating Android version :)
The idea was to make middleware which contains GUI to display blocks and board, and allow players to move blocks. Middleware is connected with game (another app), which count score and do another basic things. And here comes my question. I want to split display into two parts - one for middleware GUI and second for game e.g. displaying score.
I found Fragments, but everywhere it was used in one application. Is it possible to display two fragments from differen apps at one time?
Thanks, for help :)
Is it possible to display two fragments from differen apps at one time?
No, sorry.
You can use RemoteViews — what app widgets and custom notifications use — to pass bits of UI from one app to another, though RemoteViews are limited.
You could create your own RemoteViews-like system, marshalling instructions for how to build a UI and pass events, then use that between your apps.
On Android 7.0+, the user could enter split-screen mode, showing two activities side-by-side, and you might leverage that for what you are seeking.
You should take a look here:
https://developer.android.com/guide/topics/ui/multi-window.html
But as CommonsWare mentioned, it is only from Android 7.0 officially.
With same hacks, you can use it it lower Android versions too, but root necessary.
Or you have to use manufacturer specific APIs, if it is supported, like on Samsung devices:
http://developer.samsung.com/s-pen-sdk/technical-docs/Designing-For-The-Galaxy-Note-Creating-Multi-Window-Apps
So, I think the best option would be splitting middleware GUI for GUI and part for game, so game would send data to middleware and it would display it.
I am new in using java and android. I've read about the android application life cycle and other android programming basics. I need to create an application with 5 activities and will communicate with Bluetooth reader and smart card:
Home - In this activity the app should check if the the Bluetooth of the phone is enabled or not. This will be first activity which will display the 4 buttons below.
Begin - This will ask the user to present a smart card and will check if the card is valid or not.
Register - this will create a new account and the information will be saved in the smart card connected to the Bluetooth reader.
4.Select device - This will display the scanned/available Bluetooth reader in a List view. Once the user select an item, the application will go back to the Home page and the name of the Bluetooth device selected will be displayed in the Home page.
Device Info - This will display some information of the selected Bluetooth device.
Now, how should I implement this using listeners. From what I have read, other activity that is not visible to the user may be killed by the system to free up some memory space thus, the resources of that activity will be gone also. Is that correct? I want to implement this without having to create an instance of a reader class on each activity (if this is possible). I also thinking of creating a Parcelable class so that I can pass the reader class to other activities including the values that are needed by other activities.
Any help will be highly appreciated. Thanks in advance.
This is a pretty heavily contested subject among the Android community, but have you considered using Fragments?
If you are just getting started with Android, I think this approach would be entirely worthwhile. Although the Activity/Fragment life-cycle is pretty complex, if you are serious about wanting to learn Android, dealing with Fragments (at least for now) is almost unavoidable.
Even if you end up in the Anti-Fragment group, at least you'll have your own personal experiences from which you can come up with your own opinions.
I personally think that this approach will make your life a bit easier. If you do a Google search for "communicating between fragments" you will find a ton of examples to help get you started. When I was first getting starting, I watched a lot of the videos by slidenerd.
I just checked and he has a 3 part series about communicating between fragments, and the next video after the third one is about saving/maintaining the state of your fragments.
After you get a bit more acclimated with the basics, then I would suggest ditching the listeners and learning/using RxJava.
Hope this helps, don't hesitate to come back with more questions.
I'm trying to learn more about Android development, and I think I have hit a tough patch. Basically, I want to be able to send a "fake" key-press to the entire android system. For example, lets say I have my music player going and I want to pause the music. I should be able to send:
KeyEvent eventPause = new KeyEvent(KeyEvent.KEYCODE_MEDIA_PAUSE, KeyEvent.KEYCODE_BACK);
System wide so it will stop/pause what ever media event is playing. The problem is, I keep finding things online about an instrumentation class, and some other weird classes that I either will have trouble compiling or has been taken out of the android API.
Does anyone know a way I can do this? I have a rooted phone, so a solution that requires root is perfectly fine for me.
Thanks!
I'm having quite tough problem while developing a testing framework for android apps. The text got a bit long so the actual question is in bold for those that don't want to read the context.
Basically, what I'd like to achieve right now is to trace user activity while he's using the application as one of the features. There's my app that manages context data all the time and developer's app - the one being tested. My idea to do this was to get coordinates where user touched the screen along with taking a screenshot simultaneously. Then I'd use the coordinates to mark the spot on the screenshot to get the idea of what user was doing the whole time with the app. Take hints on user experience and trace crashes.
Non-system apps cannot take a screenshot for security reasons, but application itself can take a screenshot of its Activities without much trouble for non-rooted users, e.g. like here. My only hope here is to interfere with developers' code to implement the functionality of doing so while my testing app is running. Each Activity then would have to extend my overridden Activity instead of regular one, implement an interface, implement broadcast receiver etc.
I am going to write a library for developer who would like his app to be tested with my framework. I'd like it to do the job for me and be as non-intrusive as it's possible for him to use. How to achieve that the best way?
Ideal case would assume linking the library to project with maybe a small addition in manifest that'd get the job done and after just unlinking, removing that bit of xml in manifest for production.
That's an open question. I don't expect any bits of code, but some nifty Java trick, Android OS functionality or even completely other approach that'd solve my problem
I tried to be as clear as possible with the question, but that's a quite tough matter for me to describe so that could have turned out contrary. Don't hesitate to ask me for more details, to speak my mind more clearly or even rewrite the question. Thank you all very much for help!
I know this in general is beyond the scope of SO, but I am looking for some basic yes/no info to see if it is even feasible to proceed... I am thinking about building and Android 'note-taking/annotation' app that runs 'over' other installed Android apps, such as the web browser for example.
Essentially, while the user is browsing, my app would be running in the bg as a service, and then they could activate it which would then essentially intercept user inputs and translate those on a transparent canvas over the web browser into lines, shapes, etc. The user could then take a screen-cap of their marking with the underlying web page, which would be stored to the sd card.
This is a very good idea and a great question, but sadly, I do not believe it is possible.
The way Android is designed only one Activity can have focus at a time, while a Service could run in the background, the user would not be able to interact with it. The user can only interact with the currently active Activity.
Again, love the idea, but it is sadly not supported.
You might be able to achieve this with the WindowManager service. You can then use that to call addView() with a view of type TYPE_SYSTEM_ALERT, or possibly TYPE_SYSTEM_OVERLAY (but see the notes in the documentation about taking input focus).
I haven't tried it myself, but I've seen several apps (often dictionary apps that translate whatever words you tap on) that do overlays, and they always seem to require the SYSTEM_ALERT_WINDOW permission.