Alright, so I have an application that has a tabbed interface. What I would like to know is how I can change the currently active Activity in the tab, and then when I'm done with that Activity, go back the the original Activity, exactly as it was.
Would I be able to use something like a ViewFlipper for this? Or probably not?
If you're just concerned about what happens in the activity within that particular tab, the normal rules of activites apply, i.e. as if this activity were the only one in your application: You can either use Activity.finish() to go back to the previous activity on the stack, or use a ViewFlipper or something to randomly switch between various activities.
Another (discouraged) way would be to remove all views and then call setContentView() to inflate a new layout, but the above ways would be preferred.
Related
Consider the following scenario:
2 tabs in a parent activity. Each of them has a separate activity (please don't go about saying that this is deprecated and use fragments, I already know that). Now if I add a "OnTabChangeListener" and implement the abstract function "onTabChanged", in which cases will that function be called?
Only when the tabs are switched, i.e. I go from tab1 to tab2 (or the other way around) or also when from tab1/tab2 I go to another activity(not tab2/tab1, but something entirely different).
And is there a way to change the default behaviour?
I am currently writing an Android app for my Masters final project. The app has two Activities and both have layouts corresponding to each activity. I also have a settings activity with settings fragment but I am not concerned about this one.
Activity A currently has a Spinner and a button which when pressed will do some stuff and then launch a Activity B. Activity B displays a chart, contains a couple of actions and a button to go back to Activity A.
Neither of these activities have Fragments currently but I am curious if it would be better to include Fragments. As far as I can tell using a Fragment wouldn't hinder performance so at this point it would be a cosmetic change.
Fragments are usually needed if you need to re-use some specific layout.
E.g--> If you have an app which displays movies, you can click and get movie details.
Here it would be better to use fragments as the layout would be the same for each movie and only the content would change inside.
In your case however, since there is not need for such frequent scenarios, you really do not need fragments.
If you want to change the content without changing your activity due to some actions of user, implementing UIs inside Fragments can be useful. Just set the the desired fragment inside Activity by using its FragmentManager. However, otherwise don't think about such change (shifting lots of code/layout from activities to fragments) in the code.
A fairly common model for iOS apps seems to have a single UITabBarController, where each tab essentially holds a UINavigationController, i.e. a stack of controllers, and pressing a tab switches to the top of the corresponding stack. Can I get the same behavior on Android without a lot of custom code? (Note: I am using menus instead of tabs on Android).
After reading http://developer.android.com/guide/topics/fundamentals/tasks-and-back-stack.html the closest I can see is to have multiple tasks, each one representing a stack (akin to UINavigationController), and to use FLAG_ACTIVITY_NEW_TASK to switch to another task/stack. When I switch, is there a way to go straight to the top of the stack, or do I need to keep that piece of state myself?
One problem with keeping that state myself: I've noticed that if my app launches an Intent that starts a new process, sometimes my original app's process is killed (I think), and all of my global state is destroyed.
The only other solution I can imagine is to have a dummy Activity per stack, to push DummyActivityN essentially right before I switch away from the Nth stack, and, when switching to the Mth stack, to start activity DummyActivityM with FLAG_ACTIVITY_NEW_TASK, and then have DummyActivityM immediately finish() itself.
One last problem: as I navigate to the bottom of one of the stacks, with the back button, I would like to hit the back button once more and NOT go to another stack/task. But this seems easy to overcome by launching an Intent to go to the home screen; is there anything better?
If I understood your problem correctly, if you add this parameter to your Activity declarations in AndroidManifest.xml, the Activities that are launched by your custom menu will be only created once - keeping their state while you move around the tabs.
android:launchMode="singleTask"
I hope this helps,
Best,
-sekran
I have read about activities, stacks and launch modes, but have a hard time understanding how to apply this info to my specific problem.
Basically what I want to do is to launch a new activity, and make sure that it takes the parent's place in the stack, rather than being placed on top of it in the stack. I have the following two scenarios:
I have a login-activity. When the user logs in, a new activity is launched. When the user hits "back", I don't want him to be sent back to the login-activity, but rather to exit the program.
I have an activity that is displayed in a tabhost (or rather in a framelayout inside a tabhost, I suppose). This activity has a button, an clicking on this launches a new activity. I would like this new activity to take the parents place. That is, i don't want to open a new full-screen activity, but instead I want it to take the parent's place inside the framelayout in the tabhost. Also, I don't want a press on the back-key to lead the user back to the parent activity.
I would appreciate it if anyone could point me in the right direction. Thanks in advance.
All you need to do is call finish() in your Login Activity. That will remove it from the Activity Stack.
This is a more difficult answer. My best suggestion here would be to either use a ViewFlipper and instead of starting a new Activity you can just manage switching the views out. Alternately, if you need the button to start a new Activity, you could use an ActivityGroup and manage the Activitys that way. They both have their own complexities, so you'll need to investigate which may work better for you.
In the first case, you need to call the finish() in your login activity.
Also check this manifest settings to finish the activity when a new task is launched.
http://developer.android.com/guide/topics/manifest/activity-element.html#finish
In the second case you may use two views as frames or a ViewFlipper. Then overwrite the keyevent to handle the back button click.
http://developer.android.com/reference/android/view/View.html#onKeyDown(int,android.view.KeyEvent)
Check if the user clicked when they are in the second view and show your first view. And if they are in the first view you may want to return false on the method that you overwrite to let the system to handle the event. [Probably close that activity]
Alternatively for the second option, it might be possible in some cases to just remove tabs and use the options menu. This way you can use your activities in a normal way.
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.