2015-01-14 32 views
0

我读过许多教程,看到了一堆例子,但我有一个很难理解活动的生命周期。我明白发生了什么,我的问题是我似乎无法弄清楚它应该如何编码。例如,假设我有一个简单的应用程序,一旦按下按钮就会添加两个数字。我知道如何编写onCreate方法,但我难以知道要添加onPause,onRestart等。我只是删除变量的内容?任何帮助,将不胜感激。活动周期例如

+0

这完全取决于你的使用情况。 – Skynet

回答

0

在一个单一的应用程序像你建议你不需要重写任何该方法......除非你想之前作出的一项重要行动/任何活动过渡之后,或者当应用程序关闭/开启/去背景,就像停止服务,启动会话,甚至在应用程序运行时记录活动状态。

其重要的是你要了解所有的活动生命周期回调以及如何使用它来优化您的应用程序。

例如:您正在浏览使用智能手机相机的应用程序,并且突然收到来自朋友的电话。自动应用程序调用onPause()方法,并且(让我们假设)里面有类似myCamera.release()这些强制相机在后台停止工作。

为什么你需要做的是什么?来优化电池和内存使用情况。你不知道该应用程序是否会被杀死并迫使它关闭......所以这是一个很好的实践。

您有这方面的例子和解释上Android Developers

希望对大家有所帮助。

+0

这绝对有帮助。非常感谢你。 –

+0

随时提供适合您的所有答案。 ty – PedroHawk

1

我创造了一个例子,以帮助您了解活动的生命周期。在你启动你的屏幕在第一时间,以下三个生命周期方法被调用:

onCreate() 
onStart() 
onResume() 

每当我们按返回键,而应用程序正在运行,则下面的生命周期方法被调用:

onPause() 
onStop() 
onDestroy() 

当我们按下home键的应用程序运行时,下面的生命周期方法被调用:

onPause() 
onStop() 

当我们恢复应用程序按下主BU后tton,下面的生命周期方法被调用:

onRestart() 
onStart() 
onResume() 

我想这会帮助你。

public class MainActivity extends Activity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    Toast.makeText(this, "Activity is created", Toast.LENGTH_SHORT).show(); 
    Log.i("onCreate():","Activity is created"); 
} 

@Override 
protected void onStart() { 
    // TODO Auto-generated method stub 
    super.onStart(); 
    Toast.makeText(this, "Activity is started", Toast.LENGTH_SHORT).show(); 
    Log.i("onStart():","Activity started"); 
} 

@Override 
protected void onRestart() { 
    // TODO Auto-generated method stub 
    super.onRestart(); 
    Toast.makeText(this, "Activity is Restarted", Toast.LENGTH_SHORT).show(); 
    Log.i("onRestart():","Activity Restarted"); 
} 

@Override 
protected void onResume() { 
    // TODO Auto-generated method stub 
    super.onResume(); 
    Toast.makeText(this, "Activity is Resumed", Toast.LENGTH_SHORT).show(); 
    Log.i("onResume():","Activity Resumed"); 
} 

@Override 
protected void onPause() { 
    // TODO Auto-generated method stub 
    super.onPause(); 
    Toast.makeText(this, "Activity is Paused", Toast.LENGTH_SHORT).show(); 
    Log.i("onPause():","Activity paused"); 
} 

@Override 
protected void onStop() { 
    // TODO Auto-generated method stub 
    super.onStop(); 
    Toast.makeText(this, "Activity is Stopped", Toast.LENGTH_SHORT).show(); 
    Log.i("onStop():","Activity stopped"); 
} 

@Override 
protected void onDestroy() { 
    // TODO Auto-generated method stub 
    super.onDestroy(); 
    Toast.makeText(this, "Activity is Destroyed", Toast.LENGTH_SHORT).show(); 
    Log.i("onDestroy():","Activity destroyed"); 
} 

} 
+0

一个活动永远不会被破坏untiadb外壳 –

+0

这非常有帮助。非常感激。 –

+0

当一个活动正在运行时,如果你按回按钮,那么该活动的onDestroy()方法被调用。 –

0

从coursera实验室验证码检查

package course.labs.activitylab; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.TextView; 

public class ActivityOne extends Activity { 

    // Use these as keys when you're saving state between reconfigurations 
    private static final String RESTART_KEY = "restart"; 
    private static final String RESUME_KEY = "resume"; 
    private static final String START_KEY = "start"; 
    private static final String CREATE_KEY = "create"; 

    // String for LogCat documentation 
    private final static String TAG = "Lab-ActivityOne"; 

    // Lifecycle counters 

    // TODO: 
    // Create variables named 
    // mCreate, mRestart, mStart and mResume 
    // to count calls to onCreate(), onRestart(), onStart() and 
    // onResume(). These variables should not be defined as static. 
    int mCreate = 0 , mRestart=0 , mStart = 0 , mResume = 0; 

    // You will need to increment these variables' values when their 
    // corresponding lifecycle methods get called. 

    // TODO: Create variables for each of the TextViews 
    // named mTvCreate, mTvRestart, mTvStart, mTvResume. 
    // for displaying the current count of each counter variable 

    TextView mTvCreate , mTvRestart , mTvStart , mTvResume ; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_one); 

     // TODO: Assign the appropriate TextViews to the TextView variables 
     // Hint: Access the TextView by calling Activity's findViewById() 
     // textView1 = (TextView) findViewById(R.id.textView1); 

     mTvCreate = (TextView) findViewById(R.id.create); 
     mTvRestart = (TextView) findViewById(R.id.restart); 
     mTvStart = (TextView) findViewById(R.id.start); 
     mTvResume = (TextView) findViewById(R.id.resume); 


     Button launchActivityTwoButton = (Button) findViewById(R.id.bLaunchActivityTwo); 
     launchActivityTwoButton.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO: 
       // Launch Activity Two 
       // Hint: use Context's startActivity() method 

       // Create an intent stating which Activity you would like to 
       // start 
       startActivity(new Intent (ActivityOne.this , ActivityTwo.class)); 

       // Launch the Activity using the intent 

      } 
     }); 

     // Has previous state been saved? 
     if (savedInstanceState != null) { 

      // TODO: 
      // Restore value of counters from saved state 
      // Only need 4 lines of code, one for every count variable 

      mCreate = savedInstanceState.getInt(CREATE_KEY); 
      mStart = savedInstanceState.getInt(START_KEY); 
      mRestart = savedInstanceState.getInt(RESTART_KEY); 
      mResume = savedInstanceState.getInt(RESUME_KEY); 

     } 

     // Emit LogCat message 
     Log.i(TAG, "Entered the onCreate() method"); 

     // TODO: 
     // Update the appropriate count variable 
     // Update the user interface via the displayCounts() method 
     mCreate ++; 
     displayCounts(); 

    } 

    // Lifecycle callback overrides 

    @Override 
    public void onStart() { 
     super.onStart(); 

     // Emit LogCat message 
     Log.i(TAG, "Entered the onStart() method"); 

     // TODO: 
     // Update the appropriate count variable 
     // Update the user interface 
     mStart++; 
     displayCounts(); 


    } 

    @Override 
    public void onResume() { 
     super.onResume(); 

     // Emit LogCat message 
     Log.i(TAG, "Entered the onResume() method"); 

     // TODO: 
     // Update the appropriate count variable 
     // Update the user interface 
     mResume ++; 
     displayCounts(); 

    } 

    @Override 
    public void onPause() { 
     super.onPause(); 

     // Emit LogCat message 
     Log.i(TAG, "Entered the onPause() method"); 

    } 

    @Override 
    public void onStop() { 
     super.onStop(); 

     // Emit LogCat message 
     Log.i(TAG, "Entered the onStop() method"); 
    } 

    @Override 
    public void onRestart() { 
     super.onRestart(); 

     // Emit LogCat message 
     Log.i(TAG, "Entered the onRestart() method"); 

     // TODO: 
     // Update the appropriate count variable 
     // Update the user interface 
     mRestart ++; 
     displayCounts(); 

    } 

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 

     // Emit LogCat message 
     Log.i(TAG, "Entered the onDestroy() method"); 
    } 

    @Override 
    public void onSaveInstanceState(Bundle savedInstanceState) { 
     // TODO: 
     // Save state information with a collection of key-value pairs 
     // 4 lines of code, one for every count variable 
     savedInstanceState.putInt(CREATE_KEY, mCreate); 
     savedInstanceState.putInt(START_KEY, mStart); 
     savedInstanceState.putInt(RESTART_KEY, mRestart); 
     savedInstanceState.putInt(RESUME_KEY, mResume); 



    } 

    // Updates the displayed counters 
    // This method expects that the counters and TextView variables use the 
    // names 
    // specified above 
    public void displayCounts() { 

     // TODO - uncomment these lines 

     mTvCreate.setText("onCreate() calls: " + mCreate); 
     mTvStart.setText("onStart() calls: " + mStart); 
     mTvResume.setText("onResume() calls: " + mResume); 
     mTvRestart.setText("onRestart() calls: " + mRestart); 

    } 
}