I have 2 projects with tabbars. In each project activities are binded to tabbars. However, in the first one activities are created only for the first time I switch to certain tab (I suppose this behaviour is normal). In the second one onCreate is called each time I switch to a tab regardless the fact whether instance of this activity was created before.
I cant understand why 2 apps (2 projects = 2 apps) behave in such a different way. In neither project I tried to archieve any behaviour - it happened by itself. Can anyone explain me possible reasons of different behaviours?
Without seeing your code we can only guess.
Perhaps you are calling finish() on your activities in the one app and not in the other.
Related
I am tasked with building an app that does not have a fixed screen order.
When started the app will contact a server an get a list of actions to perform.
So one launch could be
screen 1 - screen 5 - screen 7
next time
screen 3 - screen 5 - screen 9
I tested a simple app that does that; my Main activity starts the needed activity and that activity returns to the main activity, which starts the next needed activity.
In other words, all activities return to the Main activity which then determines which activity to start next.
It seems to work fine, but I have read that Activities should only be used for UI, my Main activity had no UI, it just controlled which activity happened next. So should I be using a different approach?
I am brand new to Android, but have decades of Java experience.
In case it makes a difference, I am using AS 3.0.1
Thanks
I'd suggest one activity and implementing your "screens" as fragments. This makes it really easy to code your logic for what screen comes next. You are just adding / replacing fragments and not starting and tracking result codes of activities. The business logic for each screen can still be fully encapsulated in the fragment.
I just want to ask why my app crashes with the following conditions.
I am working with fragments with only one activity. In my fragment, say FragmentA, I create views dynamically (inflating them). They work fine. But when I press home button, and go to the app again, I expect that FragmentA will be displayed but unfortunately, it throws NullPointer.
There is no errors in my android phone[GingerBread] but on my Tab, this error happens. Any help will be higly appreciated. Thanks.
You are not providing enough information to reliably answer this question, but the difference your're seeing is probably related to different behaviour of the garbage collector on the two different devices. As they are running on two different devices they will also behave differently.
The null-pointer you are seeing I would guess stems from wrongful use of member variables in your fragment.
When you go to the home screen and back your fragment will (or more accurately CAN) be recreated. Read: destroyed and re-instantiated using the argument-less constructor.
So make sure your fragment(s) are properly saving any needed state in onSaveInstanceState and restore this information in OnCreate/OnCreateView or whatever you're using.
This is a bit of a desperation call for some good advice.
I began doing a project which involves Navigation drawer as the main menu for the application. After looking at the Android tutorial I followed the example and start developing on top of that. That example is a single activity that replaces the fragments depending on the selected option in the drawer. In part, I followed this design because if I launched Activities the drawer was lost and the Activity would appear with the launching transition and didn't look nice at all.
Now, my project is not a small one. During development I faced several issues like:
onResume not being called on the Fragments (due to not being attached to several activities but one instead.
All the data between Fragments should pass through the single Activity
Managing the Options Menus in the ActionBar became a real pain.
And many others I don't recall now
Now I am facing a new issue. In one of the fragments I need to have a Spinner that will switch fragments inside this one. And of course, the fragment will need to change the navigation mode in the action bar. This was a major headache to develop, but now I am facing a bigger problem with some fragments inside losing the activity context (like if they were detached).
After so many problems I just decided to switch back the whole app to Activities (this is a custom app that will run in just 1 tablet model, so no worries about fragmentation). So, in short I am looking for advice on the less painful way to do this.
I am on a extremely tight deadline that lead me to start implementing without designing (like a complete noob). Now I am being hit with so many issues that, if I didn't need the money, I would cancel this project at once.
Please help!
I can give a little advice, but sadly your situation can't really be solved by any one answer here.
First off, switching from a Fragment design to an Activity design is a lot easier than switching the opposite way. You can actually use all of the fragments you had before, and just have each activity loading only 1 of the fragments (or multiple if you prefer).
Also, when handling Intents (starting new activity), after the startActivity() call you can call overridePendingTransition() to make the launching transition whatever you want (or remove it completely).
I'm looking for the the best way to reproduce, in an Android app, the behavior of the iPhone UiNavigationController within an UITabBarController.
I'm working on this Android app where I have a TabActivity and 4 tabs. I've already gone through a lot of posts regarding the use of activities and tabs and how it's not a good idea to use activities for everything, which seems fair enough. I decided to use one Activity on each tab anyway, since it makes sense in my application.
However, in one of those activities I have a deep navigation tree with more than one branch and up to 12 different views the user can go through.
The problem is: Android controls the navigation through activities inside an app, if you click the back button it will go to the previous one, but if I'm navigating through views, using one Activity, and I click back, it just finishes it. So how can I have a smooth navigation behavior between views in an Activity?
I had implemented this using a TabActivity with FragmentActivity as each tab. Utilizing Fragments API you can organize the code just like you would be using 12 different activities, still using only 1 for each tab in fact. Fragment's framework will handle back key press for you to show previous fragment instead of closing the entire activity.
There are some problems with such approach, for example, there's no MapFragment, but the workarounds can be found here on SOF.
You will need Android Support Package if your minimum SDK version is lower than 3.0.
Well I know very little about UiNavigationViewController, but I guess you want something to navigate between different Views. As you are using TabActivity, every tab should load into a separate Activity.
But since you want to branch it out, using that many Activities is not a perfect solution, neither the ActivityGroup too. The better solution, as per my opinion(I have run into similar problem once) is to have the main or root tabs loads into separate Activity, but for their branches, use the ViewFlipper, which flips the Views. So the whole Layout(Subclass of View) can be flipped.
You may run into some problem while flipping more than two Views (as what people say, though I never had any problem). So in that case you can use layout.setVisibility(View.GONE) to hide the layout and just change it with View.VISIBLE for next view.
And about the concerns of back button, you need to store the last used View or Activity into a variable, and in the override of onBackPressed(), just need to call them.
There might be better solution than this, not that I can remember, but yeah it's the easiest solution I can come up with.
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