DemoActivity
public class DemoActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//加載布局
initView();
}
private void initView() {
}
@Override
protected void onPause() {
//被掛起的時候加載數(shù)據(jù)罗心,
super.onPause();
initData();
initClick();
}
private void initData() {
}
private void initClick() {
}
@Override
protected void onDestroy() {
//退出的時候釋放內(nèi)存
super.onDestroy();
}
@Override
protected void onRestart() {
//重新啟動
super.onRestart();
}
}
DemoFragment
public class DemoFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//返回視圖,返回哪個View含思,不要進行耗時操作
return super.onCreateView(inflater, container, savedInstanceState);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
//同樣不要進項耗時操作崎弃,進項一些加載布局的操作
super.onViewCreated(view, savedInstanceState);
initView();
}
private void initView() {
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
//可以進行一些稍稍費時的操作,加載數(shù)據(jù)和點擊事件在這個里面進行
super.onActivityCreated(savedInstanceState);
initData();
initClick();
}
private void initClick() {
}
private void initData() {
}
}
和Activity以及Fragment的生命周期有關。