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

ساخت وبلاگ
همیشه حس میکردم یه چیزی یه جای از برنامم نامیزونه... چون پرفومنسش نسبت به برنامه های "خارجی" بزرگتر پایین بود
سورس برنامه های بقیه(ایرانیا بیشتر) رو هم که نگاه میکردم میدیدم که خیلی از اونا هم این مشکلو دارن...

بعد از کلی تحقیق بلخره یه مقاله از پروفسور Andrew T. Campbell پیدا کردم که توضح کاملی راجب Activity Lifecycle (باز و بسته شدن برنامه های اندروید) داده بود! اونم تو یک صفحه دره پیت و ساده Heart

شیوه درست صدا زدن 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 و امثالش میشه... پس حتماً رعایت کنید.
(که کندی و ناهماهنگی UI ش شامل حال من میشد)

منبع: پروفسور Andrew T. Campbell
http://cs.dartmouth.edu/~campbell/cs65/l...ure05.html
اطلاعات بیشتر:
http://grepcode.com/file/repository.grep...ivity.java

آدمی در عالم خاکی نمی آید به دست / عالمی دیگر بباید ساخت وز نو آدمی

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

برچسب : نویسنده : استخدام کار phpco بازدید : 128 تاريخ : شنبه 29 اسفند 1394 ساعت: 9:41