As part of my widget, I use an instance of the Camera object.
This is what I want to do. The user will click on my widget, I get an instance of the Camera(if it's not already stored), use it, then store it. If they click the widget again, I want to use that same instance that I used previously.
Is this possible?
EDITED: I can't release the Camera(android.hardware.Camera) until the user clicks on the widget the second time. So the user clicks on the widget the first time, I get the camera and hold on to it until they click the widget again.
The problem I am running into is on the second click, I am trying to get the Camera again, which I can't because I currently have it in use.
No, it is not possible for an app widget to "store" arbitrary objects.
If the "Camera" object is android.hardware.Camera, you should be using it and releasing it as soon as you are done, anyway, so other applications can use the camera.
Ok, so I figured this out, I think.
If I make a static class object for the widget class, it seems to keep it in memory.
private static Camera camera = null;
Then setting it in code..
camera = Camera.open();
does seem to keep it in memory for use later. I don't know if that's really inefficient or not, but that's the only way I could get it to work.
Related
I have the following structure for my app, where ChatScreen contains the ChatHeader and the ChatFooter.
---ChatHeader.dart --- MessageDao.dart
ChatScreen.dart ---
---ChatFooter.dart
In the ChatHeader widget, I call another widget called MessageDao.dart. This widget generates the button that the user can press on the ChatHeader widget to play audio.
In the ChatFooter widget, this widget contains a different button that the user can press to record using their mic.
When I play audio using the button from ChatHeader -> MessageDao, this works just fine. However, if I click the button in the ChatFooter widget to start recording while in the middle of playing audio from the first button, this audio doesn't stop playing. It keeps playing while I'm recording. I have a stopPlaying() function in MessageDao that I can use, but I'm not sure how to call a function in widget A from widget B.
Or in other words, how do I call MessageDao.stopPlaying() from inside ChatFooter.dart?
With this type of situation, the ideal solution is to take a page out of React and 'lift the state up'; this means that whatever method is on the child should be lifted to a higher node in the widget tree (e.g. ChatScreen) and then pass the function down to the nodes that use it (ChatHeader and ChatFooter). You could also try leveraging InheritedWidget after 'lifting state up' and avoid sending methods down a chain of widgets.
Now, this is a recurring theme with Flutter apps (and overall declarative frameworks) and is a problem that can have multiple solutions. My suggestion would be that you take a look at different state management options like bloc, provider, mobx, riverpod, rxdart, redux or event_bus_plus just to mention a few. Said libraries will give you the abstractions you need for each use case and help you avoid implementing really complex logic that you might need to solve problems similar to this one by yourself.
Of course, this is an important decision for your project so I suggest you take a good look and choose whatever you feel comfortable with or is similar to something you have worked on before.
Good luck my fellow Flutter developer, choose wisely :P
You can use some event mechanism to notify MessageDao about an event in your app, e.g., AudioRecordingStartedEvent.
On MessageDao you subscribe on the event and stop the playing audio,
On ChatFooter you fire the event.
On pub.dev a lot of libraries that implement EventBus, e.g. event_bus_plus
I need to calculate the time taken by my app to start. Do you have any ideas when to start and when to stop timing?
Use your timer at the beginning of oncreate() and stop it at the end of onResume().
According to lifecycle of an Activity.
To do this, you probably should be clear on what "start my app" means - if you are referring to an activity, then you should probably override the Activity constructor (not "onCreate" except in most cases there isn't any measurable time from the constructor before onCreate is called) and capture:
SystemClock.upTimeMillis()
then you need to create a listener for onGlobalLayout to determine when the activity is actually finished displaying on the screen and get the upTime again. For the listener, do it like in this post:
Activity lifecycle - receiving notification that layout is complete
And then take the difference between the times... however, that is really the "activity" startup time.
If you are certain your app is not running, and you want to see how long it takes to "start your app" when it is not loaded in memory at all, you should extend the Application class:
public class MyApp extends Application {
public MyApp() {
// get the time
}
}
and then override the constructor to capture the timestamp like above and store it. Your app's application class is constructed first, then activities are instantiated.
Then capture the listener time in the first activity to display, as mentioned above. That will probably tell you best how much time it took from when your app "starts" to when the user actually could see it.
There are a few things that happen between when a user action occurs that is intended to "start your app" that are not included, but you have no control over that, even though that should be included if you are trying to measure how long it takes from the user's perspective.
I'm new to android, and i don't know how to make an app that if you set things inside it it will save your progress once you close it.
for example, when use input's something into an EditText, like in a to-do list app, i want all the data of the app to be saved even after shutdown.
Thanks.
Use SharedPreferences http://developer.android.com/reference/android/content/SharedPreferences.html
Obtain them using getDefaultSharedPreferences(Context context)
I am developing an app with capturing the image as one of the feature. In my home screen i have two spinner. After selecting spinner values user can go capturing the picture by clicking take picture button. Up to this working fine. But the problem resides in Camera App Orientation.
Take Picture button launches the camera.When image get captured it saved(absolutely fine) and came back to the parent activity. But the problems is it refreshes the activity. while coming back i can see following wired things
1.)Some times it shows landscape screen(1second) and back to portait which refreshes the activity and results in resetting the Spinner values.
2.) Some times it just resets the Spinner values.
It's really annoying. I haven't got any clue to get rid this problem. I hope some of you guys will solve this.
Much Appreciated.
It's good to be prepared for anything when you're launching an activity in another app (Camera). The activity could return bad data, the screen orientation could be changed, or it could be a very long time before the user returns to your app. From your description, it sounds as though the orientation is changing every time the Camera app is launched.
Android has built-in state management methods to handle this type of scenario. You can override the onSaveInstanceState() method to store your activity's state (e.g., spinner values), and then restore that state in onCreate(). (Example here.) This will handle the case in which you're launching the Camera app, or your user presses the Home button and then returns to the app, etc.
EDIT:
onRestoreInstanceState() is only called if your activity is killed due to memory pressure and then recreated later. It's much more reliable to use the Bundle passed to onCreate() (which is called again after device rotation) instead. That Bundle will contain everything that was stored in onSaveInstanceState().
If you need to postpone your work until onResume(), you'll want to use onCreate() to populate some member variables in your Activity class, and then make use of those variables in onResume().
How can I make an activity which comes as a splash screen before the app starts and waits for a specific time like 3 seconds in which the user have to draw the unlocking pattern to get in to the application?
If the user is unable to draw the right pattern, the application should open a simple web browser or another activity which is not the part of the application.
Also, after putting in the correct locking pattern, how can a user change the locking pattern so that he can use the new pattern next time he uses the application?
Start by designing an activity that displays the splash screen. Then extend it with simple time-out logic that opens the web browser and calls finish() (to exit the splash screen activity). Then extend the activity (or perhaps the view showing the splash image) to capture user input and compare it to a pre-defined pattern. (I suspect that this is the core of your question. You will have to override onTouchEvent; capture the coordinates of ACTION_DOWN, ACTION_MOVE, and ACTION_UP events; and compare the movement to your pre-defined pattern.) If the correct pattern is recognized, cancel the timer and start the new activity (with an Intent) and finish() the unlock activity.
To allow the user to change the locking pattern, you'll need to move the predefined pattern to the app's shared preferences or to some other modifiable location (such as data base or file). Then define an activity that prompts the user to define the pattern using whatever method you like (such as drawing the new pattern and capturing it using the same techniques as above) and which overwrites the stored pattern. Finally, rewrite your original splash screen activity to use the stored pattern instead of the predefined pattern (and, if the stored pattern isn't found, to initialize it with the predefined pattern).
Note that these two activities—unlocking the app and defining an unlock pattern—are separate from anything else in your app (except maybe a button or menu to let the user get to the pattern definition activity).
Or, you could use the Android Open Source Project's code:
http://code.google.com/p/android-lockpattern/source/browse/src/group/pals/android/lib/ui/lockpattern/widget/LockPatternUtils.java?r=7470bc287cba61198430e3d8aff32196bb5824a0