یوشا

دست نوشته ها و تجربیات شخصی

یوشا

دست نوشته ها و تجربیات شخصی

شهید دکتر مصطفی چمران: می گویند تقوا از تخصص لازمتر است، آنرا می پذیرم، اما می گویم آنکس که تخصص ندارد و کاری را می پذیرد بی تقواست!

طبقه بندی موضوعی
تبلیغات
Blog.ir بلاگ، رسانه متخصصین و اهل قلم، استفاده آسان از امکانات وبلاگ نویسی حرفه‌ای، در محیطی نوین، امن و پایدار bayanbox.ir صندوق بیان - تجربه‌ای متفاوت در نشر و نگهداری فایل‌ها، ۳ گیگا بایت فضای پیشرفته رایگان Bayan.ir - بیان، پیشرو در فناوری‌های فضای مجازی ایران

شیوه درست صدا زدن 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://www.cs.dartmouth.edu/~campbell/cs65/lecture05/lecture05.html

نظرات (۰)

هیچ نظری هنوز ثبت نشده است
کاربران بیان میتوانند بدون نیاز به تأیید، نظرات خود را ارسال کنند.
اگر قبلا در بیان ثبت نام کرده اید لطفا ابتدا وارد شوید، در غیر این صورت می توانید ثبت نام کنید.
شما میتوانید از این تگهای html استفاده کنید:
<b> یا <strong>، <em> یا <i>، <u>، <strike> یا <s>، <sup>، <sub>، <blockquote>، <code>، <pre>، <hr>، <br>، <p>، <a href="" title="">، <span style="">، <div align="">