Wednesday, July 8, 2015

Android activity lifecycle

Android activity lifecycle - Hallo sahabat Google Android Developer Tutorial, Pada Artikel yang anda baca kali ini dengan judul Android activity lifecycle, kami telah mempersiapkan artikel ini dengan baik untuk anda baca dan ambil informasi didalamnya. mudah-mudahan isi postingan yang kami tulis ini dapat anda pahami. baiklah, selamat membaca.

Judul : Android activity lifecycle
link : Android activity lifecycle

Baca juga


Android activity lifecycle

http://stackoverflow.com/questions/4553605/difference-between-onstart-and-onresume

up vote217down voteaccepted
+50
Why can't it be the onResume() is invoked after onRestart() and onCreate() methods just excluding onStart()? What is its purpose?
OK, as my first answer was pretty long I won't extend it further so let's try this...
public DriveToWorkActivity extends Activity
implements onReachedGroceryStoreListener { 
} 
 
public GroceryStoreActivity extends Activity {} 
PLEASE NOTE: I've deliberately left out the calls to things like super.onCreate(...) etc. This is pseudo-code so give me some artistic licence here. ;)
The methods for DriveToWorkActivity follow...
protected void onCreate(...) {
openGarageDoor
();
unlockCarAndGetIn
();
closeCarDoorAndPutOnSeatBelt
();
putKeyInIgnition
();
}

protected void onStart() {
startEngine
();
changeRadioStation
();
switchOnLightsIfNeeded
();
switchOnWipersIfNeeded
();
}

protected void onResume() {
applyFootbrake
();
releaseHandbrake
();
putCarInGear
();
drive
();
}

protected void onPause() {
putCarInNeutral
();
applyHandbrake
();
}

protected void onStop() {
switchEveryThingOff
();
turnOffEngine
();
removeSeatBeltAndGetOutOfCar
();
lockCar
();
}

protected void onDestroy() {
enterOfficeBuilding
();
}

protected void onReachedGroceryStore(...) {
Intent i = new Intent(ACTION_GET_GROCERIES, ..., this, GroceryStoreActivity.class);
}

protected void onRestart() {
unlockCarAndGetIn
();
closeDoorAndPutOnSeatBelt
();
putKeyInIgnition
();
}
OK, so it's another long one (sorry folks). But here's my explanation...
onResume() is when I start driving and onPause() is when I come to a temporary stop. So I drive then reach a red light so I pause...the light goes green and I resume. Another red light and I pause, then green so I resume. The onPause() -> onResume() -> onPause() -> onResume()loop is a tight one and occurs many times through my journey.
The loop from being stopped back through a restart (preparing to carry on my journey) to starting again is perhaps less common. In one case, I spot the Grocery Store and the GroceryStoreActivity is started (forcing my DriveToWorkActivity to the point of onStop()). When I return from the store, I go through onRestart() and onStart() then resume my journey.
I could put the code that's in onStart() into both onCreate() and onRestart() and not bother to override onStart() at all but the more that needs to be done between onCreate() -> onResume() and onRestart() -> onResume(), the more I'm duplicating things.
So, to requote once more...
Why can't it be the onResume() is invoked after onRestart() and onCreate() methods just excluding onStart()?
If you don't override onStart() then this is effectively what happens. Although the onStart()method of Activity will be called implicitly, the effect in your code is effectively onCreate() -> onResume() or onRestart() -> onResume().



Demikianlah Artikel Android activity lifecycle

Sekianlah artikel Android activity lifecycle kali ini, mudah-mudahan bisa memberi manfaat untuk anda semua. baiklah, sampai jumpa di postingan artikel lainnya.

Anda sekarang membaca artikel Android activity lifecycle dengan alamat link https://googleandroiddevelopertutorial.blogspot.com/2015/07/android-activity-lifecycle.html

Artikel Terkait

Android activity lifecycle
4/ 5
Oleh

Berlangganan

Suka dengan artikel di atas? Silakan berlangganan gratis via email