<pre name="code" class="java"><strong>MainActivity.java源码:</strong>
package com.ctcc.fragmenttest.app;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends Activity {
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG,"onCreate");
setContentView(R.layout.activity_main);
}
@Override
public void onStart(){
super.onStart();
Log.d(TAG,"onStart");
}
@Override
public void onResume(){
super.onResume();
Log.d(TAG,"onResume");
}
@Override
public void onPause(){
super.onPause();
Log.d(TAG,"onPause");
}
@Override
public void onStop(){
super.onStop();
Log.d(TAG,"onStop");
}
@Override
public void onDestroy(){
super.onDestroy();
Log.d(TAG,"onDestroy");
}
}
<pre name="code" class="java"><strong>activity_main.xml源码:</strong>
</pre><pre name="code" class="html"><LinearLayout 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"
tools:context="${relativePackage}.${activityClass}">
<TextView
android:text="@string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<fragment
android:id="@+id/fragment"
android:name="com.ctcc.fragmenttest.app.Fragment1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
</LinearLayout>
<strong>Fragment1.java源码:</strong>
package com.ctcc.fragmenttest.app;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by Zouyiran on 2015/7/26.
*/
public class Fragment1 extends Fragment {
private static final String TAG = "Fragment1";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saveInstanceState){
Log.d(TAG, "onCreateView");
return inflater.inflate(R.layout.fragment1, container, false);
}
@Override
public void onAttach(Activity activity){
super.onAttach(activity);
Log.d(TAG,"onAttach");
}
@Override
public void onCreate(Bundle saveInstanceState){
super.onCreate(saveInstanceState);
Log.d(TAG,"onCreate");
}
@Override
public void onActivityCreated(Bundle saveInstanceState){
super.onCreate(saveInstanceState);
Log.d(TAG,"onActivityCreated");
}
@Override
public void onStart(){
super.onStart();
Log.d(TAG,"onStart");
}
@Override
public void onResume(){
super.onResume();
Log.d(TAG,"onResume");
}
@Override
public void onPause(){
super.onPause();
Log.d(TAG,"onPause");
}
@Override
public void onStop(){
super.onStop();
Log.d(TAG,"onStop");
}
@Override
public void onDestroyView(){
super.onDestroyView();
Log.d(TAG,"onDestroyView");
}
@Override
public void onDestroy(){
super.onDestroy();
Log.d(TAG,"onDestroy");
}
@Override
public void onDetach(){
super.onDetach();
Log.d(TAG,"onDetach");
}
}
<pre name="code" class="java"><strong>fragment1.xml源码:</strong>
</pre><pre name="code" class="html"><LinearLayout 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"
tools:context="${relativePackage}.${activityClass}">
<TextView
android:text="@string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
通过Andoird DDMS 的Logcat参看运行结果: