Create new instance on launch - java

I'm fairly new to android, but I've noticed that pretty much every tutorial begins with this:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
But I don't want the state to persist throughout relaunching the app. I want the user to start at the beginning if they relaunch the app. How can I achieve this?

In Android, state management is up to you during the Activity lifecycle. The onCreate, onPause, onDestroy and other lifecycle methods are all available to save and restore activity state and do other things. If you don't want your app to save any state, whether it be text boxes or animations or what not then don't capture it and restore it in these events.
Bundle savedInstanceState is only set by you when the activity is paused or stopped allowing you to store state and grab it in the onCreate or onResume methods, but that also happens on orientation change of the layout (the user tips from portrait to landscape) and then you probably do want to save state details in that Bundle.
And of course you could always reset any fields overriding onResume.
Also, the Activity Launch Modes might be worth looking at. A lot of times I will set my main activity to singleTop to have only one instance launched at any given time.

protected void onPause()
{
finish();
}
It will kill the activity as it pauses.and it will create a new instance each time.

Related

When and why override onResume method in MainActiviy?

What is the reason to overide onResume? Can we avoid it?
#Override
protected void onResume() {
super.onResume();
When is mandatory to override that method in MainActiivity?
Because sometimes I see it is used sometimes not.
first of all you must know about android lifecycle link https://abhiandroid.com/programming/activity-life-cycle
onResume();
can be called in two different places after onStart(); when the user start interacting with your activity and data in the activity is visible to him
or after onPause();
when your activity goes to the background then you can implement it here to add some logic which is needed for your application .
Please reference this link:
https://developer.android.com/guide/components/activities/activity-lifecycle#onresume
When the activity enters the Resumed state, it comes to the foreground, and then the system invokes the onResume() callback. This is the state in which the app interacts with the user. The app stays in this state until something happens to take focus away from the app. Such an event might be, for instance, receiving a phone call, the user’s navigating to another activity, or the device screen’s turning off.
When the activity moves to the resumed state, any lifecycle-aware component tied to the activity's lifecycle will receive the ON_RESUME event. This is where the lifecycle components can enable any functionality that needs to run while the component is visible and in the foreground, such as starting a camera preview.
When an interruptive event occurs, the activity enters the Paused state, and the system invokes the onPause() callback.
If the activity returns to the Resumed state from the Paused state, the system once again calls onResume() method. For this reason, you should implement onResume() to initialize components that you release during onPause(), and perform any other initializations that must occur each time the activity enters the Resumed state.

Where is the difference between onCreate and onStart if both are called upon Activity change anyway? What's the purpose?

I've searched through dozens of Stackoverflow posts and the android doc but just couldn't find the answer.
According to the accepted answer of this SF-post the onCreate method runs when the activity is first created. It also notes that in here views are supposed to be created and list data is being binded.
Then the onStart Method runs but here's the issue. Where's the difference? If you do everything inside of onCreate, switch activities, your app will still display the same data, regardless whether you put the app in the background or switched activities.
So if you declare views in onCreate, what do you do in onStart? initiliaze the views to their R.id.view ? Fetch data?
onResume I suppose is then used for listeners since it's the gas and brake according to this SF-posts accepted answer.
onCreate() is called when the activity is first created. onStart() is called whenever the activity becomes visible, which includes when it is first created (after onCreate()) and after it is coming back to the screen from being stopped (e.g., another activity took over the screen).
So:
Put code in onCreate() that needs to happen when the activity is created (and use onDestroy() to clean it up)
Put code in onStart() that needs to happen either when the activity is created or when the activity returns to the foreground (and use onStop() to clean it up)
Frequently, we do not do anything special when the activity returns to the foreground, in which case you do not need to worry about onStart() or onStop().

What is the difference between activity visible liftime and foreground lifetime?

We know that the android activity cycle has multiple phases.
between onStart() and onStop() is called the visible lifetime
between onResume() and onPause() is called the foreground lifetime
What are the the key difference between them? Please give examples if possible.
If I display an Activity on the screen and the user is interacting with it, it is both in the foreground and visible.
If I start another Activity, which is transparent and shows a dialog box over the previous Activity, then the new Activity (the dialog box) is in the foreground and the old Activity is not in the foreground but still visible.
between onStart() and onStop() called visible lifetime that mean that the activity is visible either entire activity or partially visible and the user can see it on the screen and interacte with
between onResume() and onPause() called foreground lifetime that your activity is full visible and running and have full focus .
UPDATE
partially visible for example if another activity come in front of the current one and it only display a dialog and a transparent background . the user can see that activity but cant interact with it
Foreground Activity: You’d think what the user is currently interacting with would be the most important thing to keep alive.
Visible Activity: You’ll find that there are situations where your activity can be visible but not in the foreground. A simple example is when the foreground activity starts a new activity with Dialog theme or a translucent activity. Another example might when you invoke the runtime permission dialog.
Please find below link for better understanding
https://medium.com/androiddevelopers/who-lives-and-who-dies-process-priorities-on-android-cb151f39044f
The Visible Lifetime: Although this is termed the "visible lifetime", the app may not be directly visible and interacting with the user at any one time if it is not in the foreground. The feature that distinguishes this lifetime is that, even if not in the foreground, the app maintains resources such that it can instantaneously return to the foreground.
The Foreground Lifetime: During foreground lifetime the activity is in front of all other activities and interacting with the user.

Android Java: Understanding Activity Life?

I'm new to android development and I get how the android has an activity life.
If I have an app and I press a button to use the phone's camera funcationality like so...
public void onClick(View v) {
// TODO Auto-generated method stub
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_VIDEO_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_VIDEO_REQUEST);
}
How does onPause() or onDestroy() and the other stuff work?
I have this outside the onCreate()
protected void onPause(){
super.onPause();
}
If I want to press the back button or press the home button, do I have to destroy or pause the camera function? If so, I'm still trying to figure out how to do so?
Thanks!
When you start new activity from your current activity then there are two possibility of your current activity
Pause
Stop
Paused:
Another activity is in the foreground and has focus, but this one is still visible. That is, another activity is visible on top of this one and that activity is partially transparent or doesn't cover the entire screen. A paused activity is completely alive (the Activity object is retained in memory, it maintains all state and member information, and remains attached to the window manager), but can be killed by the system in extremely low memory situations.
Stopped:
The activity is completely obscured by another activity (the activity is now in the "background"). A stopped activity is also still alive (the Activity object is retained in memory, it maintains all state and member information, but is not attached to the window manager). However, it is no longer visible to the user and it can be killed by the system when memory is needed elsewhere.
For example you are starting the Camera activity from your activity then your current activity will be Stop because the Camera activity will covers all your screen and your activity is not visible to camera activity.
Here is the complete description.
You are starting the Camera activity using the Intent so you do not have to handle the call back methods of the camera activity. System will manage the call back method you do not have to manage it.You just have to manage the Activity result which you will get in your activity from the Camera activity.
EDIT
And also ob course you never have to directly call any life cycle methods of the Activity.System automatically calls this method according to activity state.You just have to write your implementation in this methods to do your work.

onDestroy for making data persistent and sync with the server

I have a little problem to detect when the application is finished. I need to do some actions onDestroy like save the parameters into the database and make a final connection to the server.
The problem is that if I put the code in onDestroy its is called when the orientation changes for example. Putting
android:configChanges="orientation|keyboardHidden"
in the manifest for that activity the landscape/portrait layouts don't swap. And adding
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setContentView(R.layout.main);
}
Changes the layouts but the buttons and labels do not get the onClickListeners and the text labels correctly. How can I solve that? Thanks
The problem is that your layout items aren't initialized again because you're initializing them in your onCreate() function, and then you're disrupting them with a new layout in onConfigurationChanged().
One option is to move the initialization to a new function that gets called from both onCreate() and onConfigurationChanged().
Another option is to use the android:onclick="" (and related) attributes in your layout.
The option I would choose is different though. I would allow Android to manage orientation (and to call onDestroy()) and in onDestroy() I would install an Alarm for, say, 10 seconds (which I imagine is plenty of time to have onCreate() called again). In onCreate() I would cancel the alarm. When the alarm fires, I would perform my save actions.
Declare buttons and labels as class variable.
setContentView recreates your view, so you must rebind your data. the best approach would be a function called both from onCreate() and onConfigurationChanged(), with layout creation and bindings.
If you don't want to anything to happen when orientation changes occur, than you should not re-setContentView(). Basically you are telling your app: "DO NOTHING WHEN ORIENTATION CHANGES". So, remove the setContentView inside the onConfigurationChanged() or test for which orientation currently is active and then load desired layout resources.
When orientation changes onDestroy() is called because the changes restart your entire activity.
Read more here:
http://developer.android.com/guide/practices/screens_support.html#qualifiers
http://developer.android.com/guide/topics/resources/providing-resources.html
Orientation testing:
Setting the background of an Activity
Could you do that stuff in overriden finish() of the activity?

Categories