我读过许多教程,看到了一堆例子,但我有一个很难理解活动的生命周期。我明白发生了什么,我的问题是我似乎无法弄清楚它应该如何编码。例如,假设我有一个简单的应用程序,一旦按下按钮就会添加两个数字。我知道如何编写onCreate方法,但我难以知道要添加onPause,onRestart等。我只是删除变量的内容?任何帮助,将不胜感激。活动周期例如
活动周期例如
回答
在一个单一的应用程序像你建议你不需要重写任何该方法......除非你想之前作出的一项重要行动/任何活动过渡之后,或者当应用程序关闭/开启/去背景,就像停止服务,启动会话,甚至在应用程序运行时记录活动状态。
其重要的是你要了解所有的活动生命周期回调以及如何使用它来优化您的应用程序。
例如:您正在浏览使用智能手机相机的应用程序,并且突然收到来自朋友的电话。自动应用程序调用onPause()
方法,并且(让我们假设)里面有类似myCamera.release()
这些强制相机在后台停止工作。
为什么你需要做的是什么?来优化电池和内存使用情况。你不知道该应用程序是否会被杀死并迫使它关闭......所以这是一个很好的实践。
您有这方面的例子和解释上Android Developers
希望对大家有所帮助。
这绝对有帮助。非常感谢你。 –
随时提供适合您的所有答案。 ty – PedroHawk
我创造了一个例子,以帮助您了解活动的生命周期。在你启动你的屏幕在第一时间,以下三个生命周期方法被调用:
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");
}
}
一个活动永远不会被破坏untiadb外壳 –
这非常有帮助。非常感激。 –
当一个活动正在运行时,如果你按回按钮,那么该活动的onDestroy()方法被调用。 –
从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);
}
}
- 1. 活动周期
- 2. 活动实例的生命周期
- 3. Android活动生命周期
- 4. android活动生命周期
- 5. Android活动生命周期
- 6. 活动周期如何工作?
- 7. 通过Android活动生命周期保存实例变量值
- 8. 单个实例活动的Android片段生命周期
- 9. Android活动生命周期与流程生命周期
- 10. Android项目生命周期 - 活动生命周期之前(Activity.onCreate())
- 11. 活动生命周期中的问题
- 12. Android:关闭后的活动周期
- 13. Tabwidget中的活动生命周期
- 14. Android活动生命周期问题
- 15. 更多活动生命周期混淆
- 16. 活动生命周期问题
- 17. Android活动生命周期回调
- 18. 监视活动的生命周期
- 19. 活动生命周期相关问题
- 20. 活动中的片段生命周期
- 21. 活动周期 - 报关序列
- 22. 周期性活动:RFC 5545 JavaScript脚本
- 23. android:AlarmManager和活动生命周期
- 24. 了解片段活动生命周期
- 25. 活动和其生命周期
- 26. 关于活动的生命周期
- 27. 混乱的活动生命周期
- 28. 骡子活动的生命周期
- 29. 活动生命周期和类属性
- 30. Android中的活动生命周期
这完全取决于你的使用情况。 – Skynet