Where to put preference based layout operations in ActionBarActivity - android

In my app I am creating a custom preference screen. This screen in reached via mainActivity (launcher). The mainActivity also shows the current status of some of these settings by reading them and laying them on views. Now, when the user will reach one of the preference screens and edit them, by design I aim to bring him back to the mainActivity (it would be intuitive to press the back button). This time however I want to show the latest edited settings.
I am used to putting layout operations in onCreate. But, in this case onCreate will only be called once when the activity starts and read the status of preference settings and lay them on screen. However, when he will open a settings activity, edit them and press back button he will not see his latest settings laid on screen as onCreate need not be called.
So, on what activity callback should I place the operations to read preferences and lay them on views.
This is based on my understanding and I may have messed up big time. Guide me, Thanks...

To anyone looking for an answer. Its pretty basic stuff. Create function in your main activity to read preferences and update your views. Now, start your preference activity using startActivityforResult() and when result arrives from that activity, run the same function inside onActivityResult(). Done.

Related

Stopping onCreate being executed on screen rotation in Android

I have an activity with 4 elements. A spinner containing a list of dates, a spinner containing a list of hours, a button and a list view. The spinner's selected items are used to form a web service URL which is called when the button is clicked and the response is shown in the list view.
The issue is if the user views the app in portrait mode, chooses a date, chooses an hour and clicks the button, the response of the web service call is shown in the list view however, if the device is rotated to landscape then the data in the list view is gone (because in order to get it there a button click is needed).
I understand that onCreate is called when the screen is rotated. I do not want to force the orientation so is there any way I can stop the list view being cleared? Note that the selected values in my spinners remain the same after rotation, it is just the response in the list view that is lost.
Simplest way to prevent activity recreation put this in you AndroidManifest
<activity android:name=".YourActivityName"
android:configChanges="orientation|keyboardHidden|screenSize">
Read this for more info - Supporting Multiple Screens
You can force activity screen orientation in AndroidManifest.xml by setting screenOrientation property:
<activity android:name=".FooActivity"
android:screenOrientation="portrait"/>
This is a half-solution. If you want to handle screen rotation, you should save and restore activity state. This is the major, royal PITA in any Android application I've seen.
The problem is, that your application logic is mixed with view code, which can be destroyed at any moment. Perfect combination, Google! It's like running a function that can disappear during execution. :)
To counter this sorry design decision you may want to move your application logic to service, which will not be destroyed when screen rotates. This 2-layer design is closer to universally accepted MVC pattern, as you separate your logic from your view. Service stays, activity attaches and detaches from service on demand, making screen rotation handling a breeze.
If you're dealing with webservices, do not try to invent your own solution for this. There is couple of nice libraries to handle this nicely, such as RoboSpice and you'll probably never come with any quick solution that is as good as those libs. Give it a try.
Also, watch this Google I/O video about developing Android REST client applications: https://www.youtube.com/watch?v=xHXn3Kg2IQE
Have your button set a static class variable, or shared preference. Then, call a method that reads that value, and does what you want. THEN, put a call to that same method, in onResume(), possibly based on a condition... I think that's what's worked for me. When you come back from configuration change or kill, onResume() should re-do whatever you had last done.

Change default Activity dynamically

I want to be able to change which Activity is run when the user runs the application.
I know how to do this in the application's manifest file, but I would like to do it programmatically after install. The reason being, I want the user to be able to choose which screen loads when he opens the application.
How can I do this? The only way I know of seems very clunky: have an essentially empty Activity which has the MAIN intent-filter - which then reads the user's settings and transfers the user to the desired Activity. This creates a lot of mess, like the back-stack needs to be considered, and the overheads of loading one activity straight after another seems wasteful of time and resources.
You cannot change launcher activity dynamically.
As you said it is possible by saving user preferences and start desired activity each time. I have tried it and it is fast enough to not show itself. Don't remember to call finish on main activity. it will solve back stack problem.
Also you can use different fragments for different activities and decide which one should be added to main activity. It may be faster. However as I said it is fast enough to start a new activity and hide main activity because it is done in onCreate method. Don't worry about that.

Modifying application workflow to use TabActivity

This question actually has two parts.
The first part:
I've been developing my first app for a couple of weeks now. I have 5 screens and everything seems well. However, I'm considering changing the app's navigation to a TabView.
I haven't delved much into it, but I'm hoping someone can save me a little bit of time. It seems that people don't generally place Activities inside each tab. They simply point the tab content to a View. This is where my major setbacks are. 1) I already have Activity classes full of code and 2) I can't quickly guess how the structure of an app using TabView looks. For example, where do I put the handler code for clicking a button on a View? Does it all just get dumped into the TabView Activity somehow?
What I would like is if you could please give me a quick synopsis of what I'm looking at doing, answers to any questions you think I may have, and point me toward some resources for creating TabView applications. A quick Google search really just shows me how to create a TabView Activity and add a couple tabs to it. The code doesn't go any deeper. For example, say I have a layout xml to show in one of my tab's content pane, where does the code go for clicking a button I have in that layout?
The second part:
I've added a TabActivity to wrap the Activities I currently have in. At the moment I have Activities populating the content of my tabs (though ultimately I'd like to do this in the most efficient fashion, which doesn't seem to be having Activities be tab content). I've noticed something rather annoying. My MAIN Activity is an Activity I wrote for my user to log in to their account. After logging in, they are taken to my Tab Activity. Here is what happens:
When I am on my Tab Activity and I "minimize" the app by clicking the Home button and then launch it again, I don't get taken back to the Tab Activity. I get taken to my log in Activity. Why? I don't have the launchMode of my Tab Activity set to singleInstance... or is it singleInstance by default? How can I make the app re-launch showing the Tab Activity (ideally by setting some parameter, assuming I'm doing something wrong, and not having to save this data off somewhere and reading it and programmatically telling it what to go to)?
Thank you for all your time and help
I don't have a comment on the advisability avoiding the use of sub-activities in TabActivity. As for handlers -- if you aren't going to embed views instead of activities, then all the android:onclick type handler settings in your layout XML will call methods on the TabActivity. This is because they go to methods on the views' Context, which is the generally the nearest containing Activity. If you want to split your code up further without using Activities, I believe you'll have to use findViewById calls on the tab content views after you've set them up, and bind the handlers manually from there in your code.

Android back button does not restart activity?

My app intiates an activity. On the click of a button, the app opens up the browser with a webpage. When I hit the back button, it comes back to my initial activity screen, but does not resume or restart the activity.
When I put all the layout code and activity code in onResume instead of onCreate, the activity gets restarted.
My question is whether this is the right way to go about it? Can I use onResume to draw my layout and initiate the activity, or is this poor design? When the browser fires up, does the initial activity forget its layout?
Please let me know what you suggest.
Thanks
Chris
Mostly you should read about the Activity Life Cycle.
It is fine to initialize in onResume as long as you only do it once. Either have a dedicated hasInitialized member or check some other value that will have equivalent meaning, and do not initialize again if it is set.

How can I keep current screen (contentView) when l Iaunch a new Activity

I have an NoContentViewActivity which has no Content View (i.e. I did not call setContentView() in the onCreate of my Activity).
My question is how can I keep the content view of the launching activity on the screen? Right now, I am getting a blank screen whenever I launch NoContentViewActivity? I want the content view of the launching activity (the activity which start the NoContentViewActivity) to show on the screen.
Thank you for any help.
Excuse me, people, but my guess is that hap497 wants exactly the thing he wants. There is a bunch of situations where invisible activity would fit while Service will not.
Imaging you want to show a set of dialogs, each next of them is shown after the previous one based on the user choices. And imaging you want to have this (exactly the same) functionality to be available when pressing different buttons on different (lots of them) activities.
To write the same dialog processing logic would be an overkill whether the transparent activity will deal nicely...
Anyway, as stated above, all you need to do is to specify:
android:theme="#android:style/Theme.Translucent"
or
android:theme="#android:style/Theme.Translucent.NoTitleBar"
(if you do not want a titlebar either)
It sounds like you'd be better off using a Service than an Activity. Activities are for code that is to be viewed; Services are for code that runs without a UI, which is what it sounds like you want.
An Activity with no Views assigned is an empty black screen, so it will still obscure the calling Activity. You could make your Activity transparent by assigning it a transparent theme:
android:theme="#style/Theme.Translucent"
Keep in mind though, that your invisible Activity will have focus, so the user won't be able to interact with the Activity underneath.
Why do you want to create a fully transparent Activity? As Daniel suggests, a Service might be a better solution to your problem if you genuinely don't want any user interaction.

Categories

Resources