0

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;
    }

`

Steve
  • 427
  • 4
  • 14

1 Answers1

2

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.

Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
  • So i should use onActivityCreated on my main activity and call process the fragments on this function? This will avoid that onCreate from fragments will get called first? – Steve Sep 03 '12 at 15:01
  • Your `Fragment` probably shouldn't be interacting with the `Activity` too much, but if it does then you should do it in or after `onActivityCreated`. – Alex Lockwood Sep 03 '12 at 15:03
  • I am afraid i am not understanding you see all the code that needs to be processed is the function onCreateView and i guess i cant move it from there, so how can i use the onActivityCreated? Sorry for being noob but i am kinda lost now – Steve Sep 03 '12 at 15:12
  • Can you post your code? I don't understand which part is giving you trouble. Are you trying to access the `Activity`s views from within the `Fragment`? Because in general you don't want to do that... your `Fragment`s should have their own UI/layout so that they can be used w/o depending on the underlying activity. – Alex Lockwood Sep 03 '12 at 15:20
  • This is line that is giving me trouble ` ArrayList shorterList = new ArrayList(appsList.subList( starterPosition, enderPosition)); ` It gets me a invalid argument because when i return back to my app the main list is empty and this main list is filled by the main activity and then passed to my pageViewer adapter where the fragments are – Steve Sep 03 '12 at 15:24
  • I don't know what `App`, `appList`, `subList`, `starterPosition`, or `enderPosition` means lol... you should post your code so I can understand the context better. – Alex Lockwood Sep 03 '12 at 15:25
  • I'm still not understanding what `appsList` is, but you should be returning `view` from `onCreateView`... not `gridview`. – Alex Lockwood Sep 03 '12 at 15:41
  • appsList is List of all installed apps in android, each App object keeps the name icon and intent needed to launch the app. The problem is that i am populating this list with the main activity but since when i return to my app that is being killed because of low memory the onCreate of the fragment gets called first but just about that time the main list is empty!!! It has 0 objects in it because it wasn´t populated yet by the main activity. – Steve Sep 03 '12 at 15:45
  • Again the exception only occurs when the app gets killed and i return to my app, when it runs the first time everything is fine since the main activity is runned first and the main list gets populated – Steve Sep 03 '12 at 15:48
  • It sounds like the best fix would be to have the `Fragment` load the apps instead of the `Fragment`... is there any reason why you are not doing this? This is the recommended approach especially with the `LoaderManager`. – Alex Lockwood Sep 03 '12 at 15:53
  • Well i thought about this yeah but since there could be 1 to 10 or more fragments since it really depends of the number of viewpages i need how do i prevent fecthing all installed apps again and again? – Steve Sep 03 '12 at 15:56
  • Just found a way to move the app list fetch to view pager and it works! thank you :) I am adding your answer as accepted – Steve Sep 08 '12 at 21:49