Activity is not restarting on tab changed in android - android

I am making a activity using Tab-Host.
I have two tabs. When I start the tab-Host activity, the tab-Host opens the activity and the life-cycle of the activity is calling but when I changed the tab and again open that previous tab the activity is not getting its callback methods like resume.

I don't think there is any specific reason it should restart. For changing configuration (like rotating the device or sliding out a keyboard) there is a specific trigger because the app needs to deal with the change. But any other process should go according to the Activitvy lifeCycle
When your app goes to the background (looses focus) for any reason you get onPause() called, and when it goes back, your onResume() will be called. This is the same for when you go home and then back to your app, or when you switch activities like that. No new intent or something like that, just going back to the activity.
You should put your code that needs to run in the onResume().

Do what you need to do in the activity in onResume() instead. That will get called everytime, not just the first time it is created.
http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle

When you switch from one tab to the other and back, the first tab only gets its onResume method called since it has already had its onCreate called the first time.
You can run the code you like in your onResume method if you want anything specific to happen when it gets focus again.

Related

singleInstance launch mode: why is onCreate called after activity is brought to front?

I launch main activity, change some settings in UI, press back button and then reopen the activity. onCreate() is called again and activity is back to default state. Why is this?
I would expect only onResume() to be called since I have this in Manifest:
android:launchMode="singleInstance"
Try using moveTaskToBack(true); in onBackPressed() to make your current activity hide instead of destroying, so you can reopen it again
#Override
public void onBackPresses(){
moveTaskToBack(true);
}
I launch main activity, change some settings in UI, press back button and then reopen the activity. onCreate() is called again and activity is back to default state. Why is this?
This is expected behavior of activity life cycle. When you press back button then activity get destroyed, it will start from onCreate method.
This is because when you press back, it destroyes the activity.
I think that what you want is to keep the state when you come back to this activity, not really to have one instance of it. So you should save the information you need and then restore them in onCreate.
Single instance only means (if I remember it well) that if you launch multiple activities without finishing them, the ones that have singleInstance will go back to front when you will call startActivity() and will not call onCreate. But this means you can't have this behavior by pressing back.
Maybe you can override onBackPressed and start another activity (the one you should go back to) instead of calling super.onBackPressed(). this should do what you want but if think it will be kind of difficult to manage.
By the way remember that Activities might be killed by the system itself, so you should not rely on the fact that your activity as not been killed

Return to previous Activity without deleting current state - Android

I wrote an Android app with several Activities and a Main Activity. When I go from the Main Activity to lets say Activity B, I want to "pause" the Main Activity, when the back button is pressed it should go back and reactivate the Main Activity instead of calling onCreate().
This shall work with all Activities, so if I click on a button to start Activity B, it shall also reactivate the old Activity B instead of creating a new state with onCreate().
How can I realize this?
PS: I already tried it with parcelable, but this do only work, if I close the application or something unexpected happens.
It's always possible that the first activity may be destroyed when you start another activity. It will be recreated when you go back to it. Every app should be written with this possibility in mind. To make sure your activities can handle being destroyed and recreated, turn on the "don't keep activities" developer option.
It is by default in Android. If you Start Activity B from Activity A then activity A goes in stopped state. Below methods will be called of Activity A
onPause()
onStop()
When you tap on Back key on Activity B. Below methods of Activity A will be called.
onRestart()
onStart()
onResume()
For reactivating Activity B, you can set Activity B launch mode as singleInstance. This will make sure that only single instance of Activity B will be created. onCreate will be not be called again. onNewIntent will be called when that activity is reactivated.
Refer: http://developer.android.com/guide/topics/manifest/activity-element.html#lmode
Activities live in stack order. Each activity has its life cycle. When you close an activity (Activity B in your example) it eventually reaches onDestroy() method and removed from the stack order. It is by default in Android and there's nothing you can do about it.
What you can do is rewrite the onStop() method in Activity B and save some activity data (like text in EditText, for example) in SharedPreferences - read here. Then in your onCreate() method you can call for SharedPreferences, check if it's not empty and restore the text in the EditText by pulling appropriate value by key.

How do I determine if an activity is being launched from a back navigation versus being launched by the user in Android?

The home screen of my app needs to have different behavior when the app is being launched from outside the app versus when a back navigation occurs. For example, imagine an Android Twitter client which on launch tries to get your updated feed but when you click on an item to hit its detail page and hit back, the screen doesn't reload new feeds but is instead where you left from.
So far I've tried looking at the calling package property but this hasn't helped since it seems to be null both when app is launched by the user for the first time or when I hit back from a detail page.
When user launches it first time, onCreate() then onresume() will be called for sure. When navigating back and getting your activity from backstack, just onResume will be called.
Also if you activity has singleTop set, then you can look into onNewIntent() which will be called when you navigate back to your activity from other screens.
So, the solution I can suggest you is to set singleTop for your activity. Then while navigating back using onNewIntent(). For 1st time launch onCreate() will be called.
Sorry if I could not understand your question well..

How to handle activity coming to foreground again

The books I have are abysmal at explaining how to work with the lifecycle, there's a lot I'm missing that I'm hoping somebody can fill in.
My app structure is that when it's first started, it starts an activity full of legalbabble that the user has to accept. When he says 'ok', I start my main activity and then I call finish like this:
public void onClick(View view) { //as a result of "I accept"
Intent mainIntent = new Intent(mParent, EtMain.class);
startActivity(mainIntent); // Start the main program
finish();
}
Then in EtMain in the onCreate method, I've got some tabs and I instantiate some classes:
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mTabHost = (TabHost)findViewById(android.R.id.tabhost);
SetupTabs.setMyTabs(mTabHost, this);
mComData = new ComFields(this); // Create the objects
mDepWx = new WxFields(this, DepArr.Departure);
mArrWx = new WxFields(this, DepArr.Arrival);
mDepVs = new DepFields(this);
mArrVs = new ArrFields(this);
mTabHost.setOnTabChangedListener(new OnTabChangeListener(){
}
Questions:
The 'finish' in the first fragment should terminate the legalbabble activity so it'll never be restarted, right? And the EtMain one will remain forever (until killed externally), even if my app gets pushed to the background, right?
The way it is now, when EtMain gets pushed and later brought to the foreground (by tapping on the icon), it goes through the legalbabble screen as though it's a complete start - that's what I'd like to prevent - going thru the legalbabble screen again.
It would seem that I'd want to override onRestart in the second code fragment and put something in there to restart the app, right? That's the part I'm unclear about.
My question then is what needs to be done in onRestart. Do I have to recreate all the tabs and data in the tabs and all my object instantiations? Or is the memory state of the app saved someplace and then is restored back to the state that it was in before something else was brought to the foreground in which case not much needs to be done because all the objects and listeners will still be there?
Yes after the first activity has ended you shouldn't have to view that activity again. You could also write to the shared preferences that the user has previously seen legal info.
If you're UI object creation is in the onCreate method, this should only be called once. Pausing or resuming will not call the onCreate method again.
Unless you explicitly remove your objects and tabChangedListeners in the onPause method, you should not have to touch them in the onRestart method.
Correct, the state of the app is saved automatically. You shouldn't have to touch the onRestart method.
Hope this helps!
I think the problem is that the launch activity in your manifest is the legalbabble activity, so when you click on the icon, the system launches another one. A better architecture would be to launch the legalbabble activity it from your EtMain activity in the onCreate method of the latter, using startActivityForResult. From the docs:
As a special case, if you call startActivityForResult() with a requestCode >= 0 during the initial onCreate(Bundle savedInstanceState)/onResume() of your activity, then your window will not be displayed until a result is returned back from the started activity.
When you get the result in onActivityResult, you can call finish() if the legal stuff was declined; otherwise everything will proceed normally.
This avoids the problem that the launch activity defined in your manifest finishes when the legal stuff is accepted.
EtMain will not remain forever, if the user backs out (by pressing the BACK key) the Activity will be finished (onPause, then onStop, then onDestroy will be called).
In general you can ignore onRestore until you are doing something complicated.
Once the user has exited your application and re-enters (or presses the icon on the Homescreen), onCreate (followed by onStart and onResume) will be called for your first activity, so you do not need any logic in onRestart, your code in onCreate will do the setting up for you as it did the first time. Because of this your legal babble will appear again when the user starts the app after exiting unless you store a preference (in SharedPreferences or a database or file) to indicate you have already displayed it - in which case finish it straight away and start the main activity.
onRestart is only called when the application goes from the stopped state (onStop has been called but not onDestroy) to the started state (onStart is called but onResume has not yet).
For saving data - some components save their state automatically (e.g. EditTexts remember the text in them, TabHosts remember the currently selected tab etc). Some components will not. If you wish to save extra data then make use of onSaveInstanceState and onRestoreInstanceState. You should only use these methods to restore the state of your application or temporary data, not important things, e.g. the id of the resource what the user was looking at, what zoom level they were at etc. For things like contacts or actual data you should commit these changes to a database, SharedPreferences or other permanent storage (e.g. file) when onPause is called.
I recommend taking a look at the Android Activity lifecycle if you are confused. Or ask more questions!

Android: Does onCreate method run in tabView when we click on each tab?

In my project I have three tabs. I want to know, each time that I click a tab onCreate function runs or it runs just one time when it creates.
Actually, in one tab I want to show a picture. for the first time, default picture is shown. However, if user creates his picture (in other tabs), when he clicks this tab the new picture should be shown.
My problem is, only default picture is shown and it doesn't show new picture. I think onCreate method don't run each time that I click the tab. Am I right?
hi friend it depends up on whether the activity is still active or not, if the activity is in paused mode then only the onResume() method will be called, if the activity is destroyed then the onCreate() method will be called,
my suggestions is that if u want to write any code u can use onResume()
or else
u can explicitly kill previous activities using finish method of activity. to make sure that every time onCreate() method is invoked..
u can call this finish() in the tab class u have written for tabs before switching to other tabs
I had the same problem. Copied the same code in onResume() method & it worked as expected.

Categories

Resources