简体   繁体   中英

android fragment not restoring properly

My Fragment sometimes is not properly restored. It happens when app is no more in background cached process and then re-opened. When it happens all that is written in onActivityCreated is not happening however my System.out.println(); is called. So i end up my items are no clickable anymore. How should i fix that ?

    @Override
public void onActivityCreated(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onActivityCreated(savedInstanceState);
    System.out.println("onActivityCreated");

    relative1 = (RelativeLayout)getActivity().findViewById(R.id.r1);
    relative2 = (RelativeLayout)getActivity().findViewById(R.id.r2);
    relative3 = (RelativeLayout)getActivity().findViewById(R.id.r3);
    relative4 = (RelativeLayout)getActivity().findViewById(R.id.r4);

    relative1.setOnClickListener(this);
    relative2.setOnClickListener(this);
    relative3.setOnClickListener(this);
    relative4.setOnClickListener(this);

}

When you send your app to background and then foreground, if there is a memory problem or "Do Not Keep Acitivities" is ticked, this will be happen. Your activity and fragment will be created again. In your situation, if you keep for a long time at background, it will be probably recreated. When this happens, Activity will keep a sign of the opened fragments and will open them again. If you do not kontrol the and that fragment will be opening on Activity's onCreate and also Activity will open with its sign. You can fix this, if you control your fragment is opened or not by a flag. If it's already created, you should not open a second.. one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM