TabHost recreating instances - Android Software Development

I'm developing a simple android application using the TabActivity.
This Activity adds 3 tabs, each one with its own activity.
The problem is: when I tap each tab, the system calls onDestroy of the current activity. So, when I tap the first activity again, all the state of private fields are gone, its like it has created a new instance every time I change tabs.
I was searching around about this, and looking on the samples, but I guess that the default behavior is to keep the activity instances paused when the tab is not current.
Anyone have an idea of what I'm missing?
Thanks.

That is by design. You need to override onSaveInstanceState() and onRestoreInstanceState() in your activity class. You can store/retrieve state data there, or retrieve it int the Bundle passed into OnCreate(). See:
http://developer.android.com/reference/android/app/Activity.html#onCreate(android.os.Bundle)

Related

[Q] How to get view to recalculate data onResume()

Greetings,
I have some calculations on the main view based on some data you set in the options view. What I want to do is, when you go to the options view and change some data then click SAVE(this throws you back to the main view) that the calculations will have been made already based on the new data. The calculations are done by a service and are called once when you first open the app, so currently you need to close and reopen the app to see the new calculations.
help appreciated
I'm guessing you have a main activity for your "main view", from which you launch an options activity? If so you can simply startActivityForResult(...) from the main activity. When the user clicks SAVE just have the activity setResult(...) with an intent that contains the necessary data. Then override the onActivityResult(...) method in your main activity to handle that intent with the new data. There are also API's for activities specifically designed to manage application settings.
Also, remember that onResume() is called after onCreate() even when an activity first launches. So you could move the code that performs the calculations from onCreate() to onResume().
Hope this helps

[Q] Dialogs and Screen rotation

I saw a lot of ppl having this problem and it's something SO common to do, and the solutions I saw were ugly to the point that I bet that Google's solution for it isn't any of those.
Simple scenario, I have a few dialogues in my application being setup inside:
Code:
protected Dialog onCreateDialog(int id) {
switch(id){
case DIALOG_LOAD: return new ProgressDialog... etc.. etc..
}
}
and called with
Code:
showDialog(DIALOG_LOAD);
and dismissed with
Code:
dismissDialog(DIALOG_LOAD);
all inside the activity class.
This approach to dialogues I believe is the 'cleanest' way of doing it and just let the Activity manages the dialogues.
it works fines IF I don't rotate the device, I though because the activity was managing it, it would be all inclusive, but no, life is never that simple. If I rotate the phone when the dialogue is up, the rotation occurs, the dialogue is still visible but the moment the code try to dismiss it it gives a nice:
05-26 20:07:02.951: ERROR/AndroidRuntime(10041): java.lang.IllegalArgumentException: no dialog with id 1 was ever shown via Activity#showDialog
so...
do I have to care about dismissing and re-creating the dialogue onPause and onResume or is there anything I can do to make the activity take care of it by itself?

Location tracker problem

Hi,
I'm developing my first app and i have some problems. My app has an activity that shows a different items of a list depending of user location. So, i created a service that is also a location listener and is updating distance data of items when "onLocationChanged(Location l)" is called from location provider (gps o network).
To start the activity of this list, i check location listener can be asigned to one location provider and wait for new location data. Namely, I wait in a loop checking a variable named "isnew" that i read from a global state class instantiated in manifest. I get this object calling to getapplication() if its and activity and getApplicationContext() if its from a service.
This variable is writed from LocationListener service and readed from activity in a loop. When LocationListener onLocationChanged method is called, this method writes this global variable with a true. The loop in the activity is like:
while(!this.getActivity().isNew){}
while isNew is false the activity waits looping. The problem is that activity never leaves the loop even when onLocationChanged is called. What i'm doing wrong? Thanks.
Have you looked through nikwen's debugging guide? The same techniques can be applied to debugging when no crashes are happening.
http://forum.xda-developers.com/showpost.php?p=42601921
If you still need help, as post 9 says on the debugging guide, we need to see your code.
Did you use LocationManager? You need to use LocationManager and LocationListener together

NavigationDrawer with Fragments or Activities? Also, Fragment management...

My app uses the ActionBarCompat and NavigationDrawer, which begs the question of how is one supposed to structure the navigation? I see two options:
1) Use a single Activity and swap out Fragments.
2) Use multiple Activities, and transition between them.
The latter seems to be the correct way, however this means that each Activity must get and build the ActionBar and NavigationDrawer when called from another Activity.
How will this affect my transition animations? If I have a left-to-right transition effect when a navigation item is clicked, will it transition the entire Activity or everything below the ActionBar?
The former seems fraught with issues. A single .java class file could get really heavy and difficult to manage.
But while we're on the subject of Fragments, please consider this situation and school me on proper Fragment management:
A blank Activity with just a LinearLayout attaches a new Fragment to it that contains a Button. Inside the Fragment class, this button gets an onClickListener and when the button is clicked, another of the same kind of Fragment should be attached to the LinearLayout inside the Activity.
A) Should the Fragment class communicate to the Activity that a new Fragment should be attached? This could be done with a custom public interface and the callback being executed within the onClickListener in the Fragment class.
B) Or, should the Fragment handle attaching the new instance inside the onClickListener, making the Activity work less? By simply bridging the clicked Button and calling .getParent() until the LinearLayout is reached, then attaching it there.
The correct way is the former. You should use fragments with the navigation drawer. If you use activities you are going to setup the navigation drawer every new activity. You should open other fragments from the activity and not from the fragment itself. I believe their is a tutorial on the android developer site that explains this in depth. A quick Google search for "android fragment to fragment communication" or something like that should get you to it. I'll try and find a link later when I am on my computer if you don't find anything
Sent from my Nexus 7 using XDA Premium HD app

[Q] Callback design-pattern question

The app I'm developing has a number of master-detail scenarios using the standard design-pattern. In a detail activity I need to indicate to the listview activity that an update/delete has occurred. This so that I can update the list using the adapter's setNotifyChange method. The list activity uses a callback to its parent activity where a decision is made about type of device ie; Is it a 'twoPane' scenario? This activity then calls the Fragment passing a reference to the selected row to the detail activity so it can be updated/deleted. I've tried creating a callback from this detail activity to the listview activity but this doesn't work. I assume that is because the detail activity was not invoked directly from the list activity, but from the activity containing the device decision. Maybe I'm missing something obvious. but does anyone have any suggestions for a solution.
Thanks for any help.
javabiker said:
The app I'm developing has a number of master-detail scenarios using the standard design-pattern. In a detail activity I need to indicate to the listview activity that an update/delete has occurred. This so that I can update the list using the adapter's setNotifyChange method. The list activity uses a callback to its parent activity where a decision is made about type of device ie; Is it a 'twoPane' scenario? This activity then calls the Fragment passing a reference to the selected row to the detail activity so it can be updated/deleted. I've tried creating a callback from this detail activity to the listview activity but this doesn't work. I assume that is because the detail activity was not invoked directly from the list activity, but from the activity containing the device decision. Maybe I'm missing something obvious. but does anyone have any suggestions for a solution.
Thanks for any help.
Click to expand...
Click to collapse
After thinking more about this problem I came to the idea that creating a callback path thru the various classes involved was worth trying. Will publish the results.
Have you ever used an EventBus? Like Otto from Square? https://square.github.io/otto/
There's also, https://github.com/greenrobot/EventBus. I prefer Otto.
You just register your Fragments/Activities on onResume and unregister on onPause.
Thanks for the suggestion. Coincidently, I'd been looking at Otto without realizing its applicability to this issue. Will give it a try.
Sent from my SAMSUNG-SGH-I337 using XDA-FORUM, powered by appyet.com

Resources