Who calls the onCreate Bundle method

Back to the Android main page An activity manages a screen, i.e. a dialog or a form. When creating a project, the IDE suggests the name MainActivity as the first activity. This main activity is automatically entered in the manifest and takes control of the first screen.

Sooner or later, however, an app will want to use several activities. Each activity must be entered in the manifest.

Activity and manifest

An activity must be entered in the AndroidManifest.xml file. If you work with an IDE, it will do it automatically when you add an activity as such. With Android-Studio you can do this via the main menu.

States of an activity

Since the activity represents a screen and not the application, it also has no main function, but is called by the app. An activity can arise, disappear, be hidden, created or deleted. For each of these cases, she can be informed by implementing one of the following methods:

void onCreate (Bundle bundle) and onDestroy ()

The method onCreate is called when the activity has just been created. This method is typically overwritten for each activity, just to initialize the screen. First of all with super.onCreate (bundle) The method onCreate called the base class.

The method onDestroy () is called when the app is finally closed. So it corresponds to the counterpart to onCreate (), but is not always reliably called before the end of the activity.

The Bundle

The parameter Bundle Contains the status of the activity, which you may have changed by overwriting the method if the runtime system aborted unexpectedly onSaveInstanceState still backed up. The backup status Bundle but you can also by overwriting the method void onRestoreInstanceState (Bundle bundle) gain.

Structure of the screen

In the further course of onCreate is typically the XML layout file with setContentView loaded.
This method is called when the activity becomes visible, typically after another app has been stopped. It does not necessarily get the focus.
Another app becomes active. The current app becomes invisible, but not destroyed. All actions that are required for the display, such as animations or updating the surface, should be stopped here.

It is possible that the application can be terminated by the runtime system without further warning after onStop.

The activity will again visible. It is called before every call of onStart, except for the first one.
Another activity - presumably from your own app - comes forward. The previous activity is stopped briefly. The code in the method should be kept slim because the event can occur many times.

Since the memory can be released for other apps in this situation, the app should back up its data and restore it with onResume. This can be done with SharedPreferences, for example.

A paused activity is brought back to the foreground and interacts with the user. The code in the method should be kept slim because the event can occur many times.

onResume must restore the data that was backed up during onPause.

State of the activity Beginning end
existent onCreate onDestroy
visible onStart onStop
In the foreground onResume onPause

void onSaveInstanceState (Bundle bundle) and void onRestoreInstanceState (Bundle bundle)

The method onSaveInstanceState should be the state of the surface in an object of the class Bundle save that after the end of the activity in onRestoreInstanceState () can be restored. The method onRestoreInstanceState () should then reconstruct the surface.

Backing up and rebuilding the screen

The app can be closed by the system at any time if, for example, the memory is running low. This can be done in the background. The user will still expect to find his app in the state in which he last saw it.

To do this backup, overwrite onSaveInstanceState. The Bundle-Parameter is filled there.

@Override public void onSaveInstanceState (Bundle saveInstanceState) {saveInstanceState.putBoolean ("seen", created); saveInstanceState.putString ("name", name); // ... super.onSaveInstanceState (saveInstanceState); } The class Bundle provides separate put and get methods for the various types.

The Bundle-Parameter can be changed later from onRestoreInstanceState () but also from onCreate can be read again in order to reconstruct the activity state.

@Override public void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.main); if (savedInstanceState! = null) if (savedInstanceState.containsKey ("seen")) {created = savedInstanceState.getBoolean ("seen"); } if (savedInstanceState.containsKey ("name")) {name = savedInstanceState.getString ("name"); }}}

Base class Activity and AppCompatActivity

These methods are made available by the Activity class. The own activity extends the class Activity and overwrites the desired methods.

When Android Studio creates an activity, it expands AppCompatActivity. This ensures that the activity is also compatible with older Android versions. That's why you should do this too.

package de.willemer.myapplication; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; public class MainActivity extends AppCompatActivity {@Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main); }} The call setContentView causes the layout XML file activity_main.xml to be unfolded and displayed. The layout and the control elements of the activity are defined in this XML file.

Activity and XML layout file

There is an XML file corresponding to each activity in the layout directory. The IDE generally immediately generates an XML file for the activity. For MainActivity, the appropriate file is usually called activity_main.xml.

In the following listing, the XML file is used as the basis for your own screen when generating the activity with the setContentView function. The call thus ensures the connection between the XML file and the activity.

import android.app.Activity; public class MainActivity extends Activity {@Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main); ...} If instead of being expanded, the activity receives an app bar, i.e. a bar in which there is also access to the menu. This is important for newer Android versions because there is often no longer a standard button for the menu on the device. This is then no longer accessible to the user.

Change of activity

To start another activity, an intent is generated. The intent is passed to the method and the target activity is started. The intent therefore determines which activity is started.

The intent is filled with two activities via its constructor: The calling activity with and the target activity, the class of which is specified.

Intent intent = new Intent (this, calledActivity.class); startActivity (intent);

Activity stays on the stack

The calling activity continues, i.e. does not wait for the activity that has just started to end. It remains on the activity stack.

If the started activity is ended, for example by calling the method or by the user by pressing the back key, the starting activity appears again on the screen.

Data transfer when there is a change in activity

Frequently, data has to be transmitted to the called activity. These can be stored in the intent of the target activity by calling putExtra. A key is used for this so that the correct value can be determined for several dates. Intent intent = new Intent (this, calledActivity.class); // Fill the intent with the data intent.putExtra ("key1", "Any text"); intent.putExtra ("key2", "Nochson Text"); startActivity (intent); The data sent with putExtra are received with getStringExtra. The first parameter is used as the key. ... public void onCreate (Bundle bundle) {... Intent intent = getIntent (); String data = intent.getStringExtra ("key1"); String other = intent.getStringExtra ("key2"); ...} To transfer your own objects, you have to implement parcelable. Then the methods are used to write and retrieve with.

The called activity closes after the start of the other activity by calling finish () ;.

Calling a sub-activity

After a startActivity call, the new activity is started, but the calling activity is not blocked. So it does not wait for the results of the activity.

But if it is interested in the results of an activity, it has to call it up as a sub-activity. To do this, it calls the method.

int SUB_ACT_HOLEADDRESS = 4711; Intent intent = new Intent (this, ZielActivity.class); startActivityForResult (intent, SUB_ACT_HOLEADRESSE); The called sub-activity must store its results for the caller via the method. The status and an intent are used as parameters. Uri uriGast = Uri.parse ("content: // gast /" + gastId); Intent intent = new Intent (Intent.ACTION_PICK, uriGast); setResult (RESULT_OK, intent); finish(); Possible states of setResult:
  • RESULT_OK
  • RESULT_CANCELED: Typically, no intent is required as an additional parameter here.
The calling activity overwrites the method. @Override public void onActivityResult (int request, int result, Intent intent) {super.onActivityResult (requestCode, resultCode, intent); // request: the request // result: typically an Activity.RESULT constant // intent: delivers the returned data via intent.getData ()}

Left