شیوه درست صدازدن Superclass ها در Activity

ساخت وبلاگ

شیوه درست صدا زدن Superclass ها در Activity:


// Called after onCreate has finished, use to restore UI state
@Override
public void onRestoreInstanceState(Bundle savedInstanceState)
{
super.onRestoreInstanceState(savedInstanceState); // Always call the superclass method at FIRST.

// Restore UI state from the savedInstanceState.
// This bundle has also been passed to onCreate.
// Will only be called if the Activity has been
// killed by the system since it was last visible.
}

// Called before subsequent visible lifetimes for an activity process.
@Override
public void onRestart()
{
super.onRestart(); // Always call the superclass method at FIRST.

// Load changes knowing that the Activity has already
// been visible within this process.
}

// Called at the start of the visible lifetime.
@Override
public void onStart()
{
super.onStart(); // Always call the superclass method at FIRST.

// Apply any required UI change now that the Activity is visible.
}

// Called at the start of the active lifetime.
@Override
public void onResume()
{
// Resume any paused UI updates, threads, or processes required
// by the Activity but suspended when it was inactive.

super.onResume(); // Always call the superclass method at LAST.
}

// Called to save UI state changes at the end of the active lifecycle.
@Override
public void onSaveInstanceState(Bundle savedInstanceState)
{
// Save UI state changes to the savedInstanceState.
// This bundle will be passed to onCreate and
// onRestoreInstanceState if the process is
// killed and restarted by the run time.

super.onSaveInstanceState(savedInstanceState); // Always call the superclass method at LAST.
}

// Called at the end of the active lifetime.
@Override
public void onPause()
{
// Suspend UI updates, threads, or CPU intensive processes
// that don't need to be updated when the Activity isn't
// the active foreground Activity.

super.onPause(); // Always call the superclass method at LAST.
}

// Called at the end of the visible lifetime.
@Override
public void onStop()
{
super.onStop(); // Always call the superclass method at FIRST.

// Suspend remaining UI updates, threads, or processing
// that aren't required when the Activity isn't visible.
// Persist all edits or state changes
// as after this call the process is likely to be killed.
}

// Sometimes called at the end of the full lifetime.
@Override
public void onDestroy()
{
// Clean up any resources including ending threads,
// closing database connections etc.

super.onDestroy(); // Always call the superclass method at LAST.
}

خلاصه مطب: در متد های onRestoreInstanceState، OnStop, OnRestart, onStart، ابتدا باید Superclass صدا زده بشه و بعد کدهای شما قرار بگیره. اما در متدهای OnDestroy, OnPause, onSaveInstanceState, OnResume ابتدا باید کدهای شما قرار بگیره و بعد متد Superclass صدا زده بشه.

عدم رعایت این اولویت ها باعث کندی، crash کردن، memory leak، ناهماهنگی در UI، باگهای DataSaving و امثالش میشه... پس حتماً رعایت کنید.

(شاید درصد کمی از برنامه ها باشند که از این قائده پیروی نمی کنند، اما اون هم با آگاهی کامل از محتوا و مکانیسم Superclass ها.)

منبع: پروفسور Andrew T. Campbell
http://cs.dartmouth.edu/~campbell/cs...lecture05.html

برنامه نویس...
ما را در سایت برنامه نویس دنبال می کنید

برچسب : نویسنده : محمد رضا جوادیان programers بازدید : 160 تاريخ : شنبه 29 اسفند 1394 ساعت: 7:01