編輯:關於 Android 程式設計
看到這個標題,你可能以為是什麼高深的技巧,其實就是自訂回呼函數而已。但這個模式在實際專案裡非常常用,值得寫下來。
startActivityForResult() 啟動另一個 Activity,當棧頂的 Activity 呼叫 onActivityResult() 並 finish 之後,結果會回傳給啟動它的父 Activity。setTargetFragment() 與 onActivityResult() 在兩個 Fragment 之間傳遞資料。
這兩種方式在傳遞較複雜的結構化資料時很好用。但如果只是要傳個簡單字串,或者只是想「叫一下」讓父層做某個動作,而且不一定等到子 Activity/Fragment finish 才處理,那麼自訂回呼會直接得多。
先寫一個抽象類別,把「建立並掛上 Fragment」這段樣板包起來:
public abstract class SingleFragment extends FragmentActivity {
public abstract Fragment createFragment();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
FragmentManager fm = getSupportFragmentManager();
Fragment mFragment = fm.findFragmentById(R.id.container);
if (mFragment == null) {
mFragment = createFragment();
fm.beginTransaction().add(R.id.container, mFragment).commit();
}
}
}
之後在主 Activity 中只要繼承它並實作 createFragment(),就能輕鬆把 Fragment 掛到 R.id.container 上:
@Override
public Fragment createFragment() {
return new IndexFragment();
}
Fragment 裡只放了一個 TextView 和一個 Button,按下 Button 時就觸發回呼,讓 Activity 的實作被執行:
public class IndexFragment extends Fragment {
private IndexListener mListener;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_index, container, false);
Button button = (Button) v.findViewById(R.id.index_button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mListener.onIndexListener("Call Back to My Implementer");
}
});
return v;
}
public interface IndexListener {
void onIndexListener(String str);
}
/** 在附加到 Activity 時取得回呼實作 */
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (IndexListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " must implement IndexListener");
}
}
}
public class MyActivity extends SingleFragment implements IndexFragment.IndexListener {
private static final String TAG = "MyActivity";
@Override
public Fragment createFragment() {
return new IndexFragment();
}
@Override
public void onIndexListener(String str) {
Log.d(TAG, "From the Fragment message: " + str);
}
}
按下 Fragment 中的 Button 後,Logcat 就會印出:
From the Fragment message: Call Back to My Implementer
這看起來好像沒什麼意義,但在某些場合很重要。例如檔案管理器的搜尋功能:使用者每輸入幾個字就立刻回傳結果,不必等他把完整檔名打完。做法就是替 EditText 掛上 addTextChangedListener,在 afterTextChanged() 中取得目前的不完整字串,再透過回呼通知 Activity 去執行背景搜尋。
搞清楚「監聽者」與「被監聽者」這兩個角色之後,要寫出簡潔易懂的程式碼其實非常容易。
小提醒:使用回呼介面時,記得在 onDetach() 中把 listener 清成 null,否則 Fragment 被銷毀後仍持有 Activity 引用,容易造成記憶體洩漏。
AutoCompleteTextView 實作:讓輸入框即時推薦符合的內容
AutoCompleteTextView 動態匹配輸入內容:目標很明確——使用者一邊打字,一邊跳出符合條件的候選清單,就像搜尋引擎在你敲字時即時給出熱門關鍵字那樣。 一、
微信怎麼投訴微商賣家?微信怎麼投訴好友
微信投訴微商與不良好友的方法 若微信好友發送廣告、垃圾信息或詐騙內容,可在刪除前先進行投訴舉報。 進入對方資料頁,在頁面底部找到投訴入口。 根據實際情況選擇投訴原因,
Android Fragment 實戰:ToDoList 待辦清單詳解
ToDoList(Fragment)詳解 Fragment(碎片)可以靈活地從 Activity 上添加或刪除,能帶來良好的用戶體驗。下面就以一個待辦清單為例,完整走一遍
Android 如何保存用戶設定?以實體類為媒介存入內部儲存與 SD 卡
本次實例要做的,是把用戶的設定資訊,以實體類為媒介,保存到內部儲存或 SD 卡的檔案裡。再次打開應用時,程式會自動讀取檔案中的實體類,並對設定資訊進行套用,使其恢復到用戶