DemoActivity
public class DemoActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//加载布局
initView();
}
private void initView() {
}
@Override
protected void onPause() {
//被挂起的时候加载数据,
super.onPause();
initData();
initClick();
}
private void initData() {
}
private void initClick() {
}
@Override
protected void onDestroy() {
//退出的时候释放内存
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) {
//可以进行一些稍稍费时的操作,加载数据和点击事件在这个里面进行
super.onActivityCreated(savedInstanceState);
initData();
initClick();
}
private void initClick() {
}
private void initData() {
}
}
和Activity以及Fragment的生命周期有关。