简体   繁体   中英

Fragments onCreate after app process been killed

I have an app which is basically a launcher with a main activity and view pager fragments and i just noticed that if i launch an app from my app and my app gets killed when i return somehow the on create fragment function get´s called first that my on create main activity function.

This eventually crashes my app because i get a list of all applications using the main activity first and then each pager view fragment will get a sub list from the main list. The app runs fine when executed for the first time because android runs first the on create of the main activity and then the on create of the fragments

So how can i solve this? is there a way to call first the onCreate Main Activity function? or is there a better way do to this?

The app gives illegal argument exception when i try to create the shorter list, and since the main list is only filled be the main activity when i return to my app the main list is empty giving me the exception

The code:

`

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.grid, null);

        GridView gridview = (GridView) view.findViewById(R.id.gridview);

        int starterPosition;

        starterPosition = 16 * position;
        int enderPosition = starterPosition + 16;

        if (enderPosition > appsList.size()) {
            enderPosition = appsList.size();
        }


        ArrayList<App> shorterList = new ArrayList<App>(appsList.subList(
                starterPosition, enderPosition));

        final AppLauncherAdapter grid = new AppLauncherAdapter(ctx, shorterList);

        gridview.setAdapter(grid);

        gridview.setSelector(R.color.trans);


        return gridview;
    }

`

There is a method called onActivityCreated that you should use specifically for this purpose. onActivityCreated will always be called after the Activity 's onCreate method.

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