Miles' Blog

天涯何處無幹話,何必要講實務話

Source Code

使用兩個 Activity 和 Fragment 互傳做測試;另外並沒有使用 Support Library,以下為原始碼:

Main.java

public class Main extends Activity {
private static final String TAG = Main.class.getSimpleName();

@Override
protected void onCreate(Bundle savedInstanceState) {
Log.d(TAG, "onCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new MainFragment())
.commit();
}
}

@Override
protected void onSaveInstanceState(Bundle outState) {
Log.d(TAG, "onSaveInstanceState");
super.onSaveInstanceState(outState);
}

@Override
protected void onPause() {
Log.d(TAG, "onPause");
super.onPause();
}

@Override
protected void onStop() {
Log.d(TAG, "onStop");
super.onStop();
}

@Override
protected void onDestroy() {
Log.d(TAG, "onDestroy");
super.onDestroy();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
Log.d(TAG, "onConfigurationChanged");
super.onConfigurationChanged(newConfig);
}

@Override
protected void onRestart() {
Log.d(TAG, "onRestart");
super.onRestart();
}

@Override
protected void onResume() {
Log.d(TAG, "onResume");
super.onResume();
}

@Override
protected void onStart() {
Log.d(TAG, "onStart");
super.onStart();
}

@Override
protected void onPostResume() {
Log.d(TAG, "onPostResume");
super.onPostResume();
}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
Log.d(TAG, "onPostCreate");
super.onPostCreate(savedInstanceState);
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
Log.d(TAG, "onRestoreInstanceState");
super.onRestoreInstanceState(savedInstanceState);
}

@Override
public void onAttachFragment(Fragment fragment) {
Log.d(TAG, "onAttachFragment");
super.onAttachFragment(fragment);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.d(TAG, "onActivityResult, requestCode: " + requestCode + " , resultCode: " + resultCode);
super.onActivityResult(requestCode, resultCode, data);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
Log.d(TAG, "onCreateOptionsMenu");
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
Log.d(TAG, "onPrepareOptionsMenu");
return super.onPrepareOptionsMenu(menu);
}
}

Sub.java

public class Sub extends Activity {
private static final String TAG = Sub.class.getSimpleName();

@Override
protected void onCreate(Bundle savedInstanceState) {
Log.d(TAG, "onCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sub);

if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new SubFragment())
.commit();
}
}

@Override
protected void onSaveInstanceState(Bundle outState) {
Log.d(TAG, "onSaveInstanceState");
super.onSaveInstanceState(outState);
}

@Override
protected void onPause() {
Log.d(TAG, "onPause");
super.onPause();
}

@Override
protected void onStop() {
Log.d(TAG, "onStop");
super.onStop();
}

@Override
protected void onDestroy() {
Log.d(TAG, "onDestroy");
super.onDestroy();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
Log.d(TAG, "onConfigurationChanged");
super.onConfigurationChanged(newConfig);
}

@Override
protected void onRestart() {
Log.d(TAG, "onRestart");
super.onRestart();
}

@Override
protected void onResume() {
Log.d(TAG, "onResume");
super.onResume();
}

@Override
protected void onStart() {
Log.d(TAG, "onStart");
super.onStart();
}

@Override
protected void onPostResume() {
Log.d(TAG, "onPostResume");
super.onPostResume();
}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
Log.d(TAG, "onPostCreate");
super.onPostCreate(savedInstanceState);
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
Log.d(TAG, "onRestoreInstanceState");
super.onRestoreInstanceState(savedInstanceState);
}

@Override
public void onAttachFragment(Fragment fragment) {
Log.d(TAG, "onAttachFragment");
super.onAttachFragment(fragment);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.d(TAG, "onActivityResult, requestCode: " + requestCode + " , resultCode: " + resultCode);
super.onActivityResult(requestCode, resultCode, data);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
Log.d(TAG, "onCreateOptionsMenu");
getMenuInflater().inflate(R.menu.sub, menu);
return true;
}

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
Log.d(TAG, "onPrepareOptionsMenu");
return super.onPrepareOptionsMenu(menu);
}

}

Main.java 和 Sub.java 的內容幾乎相同,只有套用的 layout 和呼叫 Fragment 不同。

以下為監看的Callback列表(按名稱排序)

  • protected void onActivityResult(int requestCode, int resultCode, Intent data)
  • public void onAttachFragment(Fragment fragment)
  • public void onConfigurationChanged(Configuration newConfig)
  • protected void onCreate(Bundle savedInstanceState)
  • public boolean onCreateOptionsMenu(Menu menu)
  • protected void onDestroy()
  • protected void onPause()
  • protected void onPostCreate(Bundle savedInstanceState)
  • protected void onPostResume()
  • public boolean onPrepareOptionsMenu(Menu menu)
  • protected void onRestart()
  • protected void onRestoreInstanceState(Bundle savedInstanceState)
  • protected void onResume()
  • protected void onSaveInstanceState(Bundle outState)
  • protected void onStart()
  • protected void onStop()

MainFragment.java

public class MainFragment extends Fragment {
private static final String TAG = MainFragment.class.getSimpleName();

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.d(TAG, "onCreateView");

View rootView = inflater.inflate(R.layout.fragment_main, container, false);

View finish = rootView.findViewById(R.id.finish);
finish.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
getActivity().finish();
}
});

View withoutReturn = rootView.findViewById(R.id.withoutReturn);
withoutReturn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), Sub.class);
startActivity(intent);
}
});

View withReturn = rootView.findViewById(R.id.withReturn);
withReturn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), Sub.class);
startActivityForResult(intent, 10);
}
});

return rootView;
}

@Override
public void onHiddenChanged(boolean hidden) {
Log.d(TAG, "onHiddenChanged");
super.onHiddenChanged(hidden);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.d(TAG, "onActivityResult, requestCode: " + requestCode + " , resultCode: " + resultCode);
super.onActivityResult(requestCode, resultCode, data);
}

@Override
public void onAttach(Activity activity) {
Log.d(TAG, "onAttach");
super.onAttach(activity);
}

@Override
public void onCreate(Bundle savedInstanceState) {
Log.d(TAG, "onCreate");
super.onCreate(savedInstanceState);
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
Log.d(TAG, "onViewCreated");
super.onViewCreated(view, savedInstanceState);
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
Log.d(TAG, "onActivityCreated");
super.onActivityCreated(savedInstanceState);
}

@Override
public void onStart() {
Log.d(TAG, "onStart");
super.onStart();
}

@Override
public void onResume() {
Log.d(TAG, "onResume");
super.onResume();
}

@Override
public void onViewStateRestored(Bundle savedInstanceState) {
Log.d(TAG, "onViewStateRestored");
super.onViewStateRestored(savedInstanceState);
}

@Override
public void onSaveInstanceState(Bundle outState) {
Log.d(TAG, "onSaveInstanceState");
super.onSaveInstanceState(outState);
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
Log.d(TAG, "onConfigurationChanged");
super.onConfigurationChanged(newConfig);
}

@Override
public void onPause() {
Log.d(TAG, "onPause");
super.onPause();
}

@Override
public void onStop() {
Log.d(TAG, "onStop");
super.onStop();
}

@Override
public void onDestroyView() {
Log.d(TAG, "onDestroyView");
super.onDestroyView();
}

@Override
public void onDestroy() {
Log.d(TAG, "onDestroy");
super.onDestroy();
}

@Override
public void onDetach() {
Log.d(TAG, "onDetach");
super.onDetach();
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
Log.d(TAG, "onCreateOptionsMenu");
super.onCreateOptionsMenu(menu, inflater);
}

@Override
public void onPrepareOptionsMenu(Menu menu) {
Log.d(TAG, "onPrepareOptionsMenu");
super.onPrepareOptionsMenu(menu);
}

@Override
public void onDestroyOptionsMenu() {
Log.d(TAG, "onDestroyOptionsMenu");
super.onDestroyOptionsMenu();
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
Log.d(TAG, "onCreateContextMenu");
super.onCreateContextMenu(menu, v, menuInfo);
}
}

fragment_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<Button
android:id="@+id/finish"
android:text="@string/main_button_finish"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<Button
android:id="@+id/withoutReturn"
android:text="@string/main_button_without_return"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<Button
android:id="@+id/withReturn"
android:text="@string/main_button_with_return"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

</LinearLayout>

SubFragment.java

public class SubFragment extends Fragment {
private static final String TAG = SubFragment.class.getSimpleName();

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_sub, container, false);

View finish = rootView.findViewById(R.id.finish);
finish.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
getActivity().finish();
}
});

View hasReturn = rootView.findViewById(R.id.hasReturn);
hasReturn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent();
intent.putExtra("return", "I am return");
getActivity().setResult(10, intent);
getActivity().finish();
}
});

return rootView;
}

@Override
public void onHiddenChanged(boolean hidden) {
Log.d(TAG, "onHiddenChanged");
super.onHiddenChanged(hidden);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.d(TAG, "onActivityResult, requestCode: " + requestCode + " , resultCode: " + resultCode);
super.onActivityResult(requestCode, resultCode, data);
}

@Override
public void onAttach(Activity activity) {
Log.d(TAG, "onAttach");
super.onAttach(activity);
}

@Override
public void onCreate(Bundle savedInstanceState) {
Log.d(TAG, "onCreate");
super.onCreate(savedInstanceState);
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
Log.d(TAG, "onViewCreated");
super.onViewCreated(view, savedInstanceState);
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
Log.d(TAG, "onActivityCreated");
super.onActivityCreated(savedInstanceState);
}

@Override
public void onStart() {
Log.d(TAG, "onStart");
super.onStart();
}

@Override
public void onResume() {
Log.d(TAG, "onResume");
super.onResume();
}

@Override
public void onViewStateRestored(Bundle savedInstanceState) {
Log.d(TAG, "onViewStateRestored");
super.onViewStateRestored(savedInstanceState);
}

@Override
public void onSaveInstanceState(Bundle outState) {
Log.d(TAG, "onSaveInstanceState");
super.onSaveInstanceState(outState);
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
Log.d(TAG, "onConfigurationChanged");
super.onConfigurationChanged(newConfig);
}

@Override
public void onPause() {
Log.d(TAG, "onPause");
super.onPause();
}

@Override
public void onStop() {
Log.d(TAG, "onStop");
super.onStop();
}

@Override
public void onDestroyView() {
Log.d(TAG, "onDestroyView");
super.onDestroyView();
}

@Override
public void onDestroy() {
Log.d(TAG, "onDestroy");
super.onDestroy();
}

@Override
public void onDetach() {
Log.d(TAG, "onDetach");
super.onDetach();
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
Log.d(TAG, "onCreateOptionsMenu");
super.onCreateOptionsMenu(menu, inflater);
}

@Override
public void onPrepareOptionsMenu(Menu menu) {
Log.d(TAG, "onPrepareOptionsMenu");
super.onPrepareOptionsMenu(menu);
}

@Override
public void onDestroyOptionsMenu() {
Log.d(TAG, "onDestroyOptionsMenu");
super.onDestroyOptionsMenu();
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
Log.d(TAG, "onCreateContextMenu");
super.onCreateContextMenu(menu, v, menuInfo);
}
}

fragment_sub.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
android:id="@+id/finish"
android:text="@string/sub_button_finish"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<Button
android:id="@+id/hasReturn"
android:text="@string/sub_button_has_return"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

</LinearLayout>

Fragment 因 Button 不一樣,所以內容是 Button 的部分不同而已

而 Fragment 跟 Activity 要監聽的部分也有點不大一樣:

  • public void onActivityCreated(Bundle savedInstanceState)
  • public void onActivityResult(int requestCode, int resultCode, Intent data)
  • public void onAttach(Activity activity)
  • public void onConfigurationChanged(Configuration newConfig)
  • public void onCreate(Bundle savedInstanceState)
  • public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo)
  • public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
  • public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
  • public void onDestroy()
  • public void onDestroyOptionsMenu()
  • public void onDestroyView()
  • public void onDetach()
  • public void onHiddenChanged(boolean hidden)
  • public void onPause()
  • public void onPrepareOptionsMenu(Menu menu)
  • public void onResume()
  • public void onSaveInstanceState(Bundle outState)
  • public void onStart()
  • public void onStop()
  • public void onViewCreated(View view, Bundle savedInstanceState)
  • public void onViewStateRestored(Bundle savedInstanceState) (此方法有API限制API Level 17 Required,不過低版本只會報警告,仍然可以繼續執行。)
0%