如果你还在Activity的声明周期函数(onCreate、onStart)打印log来看activity的声明周期,那么你就out了!
今天楼主叫你用ActivityLifecycleCallbacks来监听activity们的声明周期。
接下来上代码:
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.lxxcaroline.applicationtest"> <application android:name="com.example.lxxcaroline.test.MyApplication" android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme"> <activity android:name=".MainActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".SecondActivity" android:label="secondActivity"> </activity> </application> </manifest>
package com.example.lxxcaroline.applicationtest; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.util.Log; import android.view.View; import android.widget.Button; public class MainActivity extends ActionBarActivity { private static final String TAG="ApplicationTest"; private Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Log.d(TAG,"MainActivity is created successfully."); button=(Button)findViewById(R.id.btn); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent=new Intent(MainActivity.this,SecondActivity.class); startActivity(intent); } }); } }
package com.example.lxxcaroline.applicationtest; import android.app.Activity; import android.os.Bundle; /** * Created by LxxCaroline on 2015/5/4. */ public class SecondActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.layout); } }
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <Button android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="button"/> </RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <TextView android:text="@string/hello_world" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RelativeLayout>
package com.example.lxxcaroline.test; import android.app.Activity; import android.app.Application; import android.os.Bundle; import android.util.Log; import com.example.lxxcaroline.applicationtest.MainActivity; import com.example.lxxcaroline.applicationtest.SecondActivity; /** * Created by LxxCaroline on 2015/5/3. */ public class MyApplication extends Application { private static final String TAG = "MyApplication"; //声明一个监听Activity们生命周期的接口 private ActivityLifecycleCallbacks activityLifecycleCallbacks = new ActivityLifecycleCallbacks() { /** * application下的每个Activity声明周期改变时,都会触发以下的函数。 */ @Override public void onActivityCreated(Activity activity, Bundle savedInstanceState) { //如何区别参数中activity代表你写的哪个activity。 if (activity.getClass() == MainActivity.class) Log.d(TAG, "MainActivityCreated."); else if(activity.getClass()== SecondActivity.class) Log.d(TAG, "SecondActivityCreated."); } @Override public void onActivityStarted(Activity activity) { Log.d(TAG, "onActivityStarted."); } @Override public void onActivityResumed(Activity activity) { Log.d(TAG, "onActivityResumed."); } @Override public void onActivityPaused(Activity activity) { Log.d(TAG, "onActivityPaused."); } @Override public void onActivityStopped(Activity activity) { Log.d(TAG, "onActivityStopped."); } @Override public void onActivitySaveInstanceState(Activity activity, Bundle outState) { } @Override public void onActivityDestroyed(Activity activity) { Log.d(TAG, "onActivityDestroyed."); } }; /** * onCreate是一个回调接口,android系统会在应用程序启动的时候,在任何应用程序组件(activity、服务、 * 广播接收器和内容提供者)被创建之前调用这个接口。 * 需要注意的是,这个方法的执行效率会直接影响到启动Activity等的性能,因此此方法应尽快完成。 * 最后在该方法中,一定要记得调用super.onCreate(),否则应用程序将会报错。 */ @Override public void onCreate() { super.onCreate(); //注册自己的Activity的生命周期回调接口。 registerActivityLifecycleCallbacks(activityLifecycleCallbacks); } @Override public void onTerminate() { //注销这个接口。 unregisterActivityLifecycleCallbacks(activityLifecycleCallbacks); super.onTerminate(); } }
05-04 10:28:03.489 24885-24885/com.example.lxxcaroline.applicationtest D/MyApplication﹕ MainActivityCreated. 05-04 10:28:03.639 24885-24885/com.example.lxxcaroline.applicationtest D/ApplicationTest﹕ MainActivity is created successfully. 05-04 10:28:03.639 24885-24885/com.example.lxxcaroline.applicationtest D/MyApplication﹕ onActivityStarted. 05-04 10:28:03.649 24885-24885/com.example.lxxcaroline.applicationtest D/MyApplication﹕ onActivityResumed. 05-04 10:28:33.361 24885-24885/com.example.lxxcaroline.applicationtest D/MyApplication﹕ onActivityPaused. 05-04 10:28:33.391 24885-24885/com.example.lxxcaroline.applicationtest D/MyApplication﹕ SecondActivityCreated. 05-04 10:28:33.411 24885-24885/com.example.lxxcaroline.applicationtest D/MyApplication﹕ onActivityStarted. 05-04 10:28:33.411 24885-24885/com.example.lxxcaroline.applicationtest D/MyApplication﹕ onActivityResumed. 05-04 10:28:33.781 24885-24885/com.example.lxxcaroline.applicationtest D/MyApplication﹕ onActivityStopped. 05-04 10:28:36.835 24885-24885/com.example.lxxcaroline.applicationtest D/MyApplication﹕ onActivityPaused. 05-04 10:28:36.875 24885-24885/com.example.lxxcaroline.applicationtest D/MyApplication﹕ onActivityStarted. 05-04 10:28:36.875 24885-24885/com.example.lxxcaroline.applicationtest D/MyApplication﹕ onActivityResumed. 05-04 10:28:37.205 24885-24885/com.example.lxxcaroline.applicationtest D/MyApplication﹕ onActivityStopped. 05-04 10:28:37.205 24885-24885/com.example.lxxcaroline.applicationtest D/MyApplication﹕ onActivityDestroyed. 05-04 10:28:38.627 24885-24885/com.example.lxxcaroline.applicationtest D/MyApplication﹕ onActivityPaused. 05-04 10:28:39.177 24885-24885/com.example.lxxcaroline.applicationtest D/MyApplication﹕ onActivityStopped. 05-04 10:28:39.177 24885-24885/com.example.lxxcaroline.applicationtest D/MyApplication﹕ onActivityDestroyed.