5

Screen 1: GridView

enter image description here

Screen 2: Detail Page

enter image description here

Task Achieve:

1) Load all the videos in gridview from the server.

2) User clicks at any position of gridview item.

3) Open and play the particular video in detail screen.

4) On vertical scroll play next or previous videos.

Current Implementation:

GridFragment {
    ArrayList<VideoPostModel> videoPostList;
    RecyclerView gridView;

    onnItemClick() {
        Intent intent  = new Intent(this, DetailActivity.class);
        intent.putExtra("data", videoPostList);
        intent.putExtra("click_index", clickedIndex);
        intent.putExtra("pagination_index", paginationIndex);
        startActivity(intent);
    }
}

DetailActivity {
    VerticlaViewPager vertiCalViewPager;
    ArrayList<VideoPostModel> videoPostList;

    onCreate() {
        videoPostList = getIntent().getParcelableArrayListExtra("data");
        clickedIndex = getIntent().getIntExtra("clickindex", 0);
        paginationIndex = getIntent().getIntExtra("pagination_index", 0);

        VideoFragmentStatePagerAdapter viewPagerAdapter = new VideoFragmentStatePagerAdapter(videoPostList);
        vertiCalViewPager.setAdapter(viewPagerAdapter);
    }
}

Problem:

If videoPostList has more data(approx 100+ objects of VideoPostModel) while passing data from fragment to activity then app crashes, as there is a limitation of sending data with intent(https://stackoverflow.com/a/37054839/3598052).

Hacky Alternatives:

1) Static arraylist

2) Arraylist in Application class

Looking for the OPTIMAL and EFFICIENT solution to achieve above functionality.

Any suggestion, reference link or code in the direction of achieving this would be highly appreciated, and thanks in advance.

Update 1:

Another solution I found is passing data with enum, but as per comments I'm not sure about it's performance. Refrence: https://stackoverflow.com/a/14706456/3598052

Chitrang
  • 5,097
  • 1
  • 35
  • 58

3 Answers3

1

I think you can write in an activity or use Arraylist in the application as you mentioned. Or it could be a library that recently appeared in the Android Jetpack. It is similar in nature to the Arraylist in application.

ViewModel objects that make it easier to manage and store data. It lets you access data at different activities or fragments in an application. You try it and hope it will be useful to you

Chanh
  • 433
  • 3
  • 12
0

You have several options. I'll put my opinion here.

Most easy would be making static list at activity or application level. Just make sure you are freeing up List memory after use by making it NULL.

Another solution I found is passing data with enum, but as per comments I'm not sure about it's performance

There would be sure some differences in each of above approaches. But that would not be measurable difference, because each approach put List in memory, use it and then free up.

Looking for the OPTIMAL and EFFICIENT solution to achieve above functionality.

Make static List, and make it NULL after use. will be most efficient and easy way. You can make List NULL in onDestroy() of your Fragment.

You can use LiveData but I think it would be not a good idea to add LiveData library just for one use in app. Also you need to understand it first. So you can go with static list.

in Activity

showFragment();
ApplicationClass.list = myList;

In Fragment

onViewCreated(){
...
setAdapter(ApplicationClass.list);
...
}

onDestroy(){
   ApplicationClass.list = null;
}

Important

It is never a good idea to pull all data at once from server. Please do pagination, which you app needs most, because there can be thousands of users online at one time.

So by that approach you will pass only few items to Fragment. then you will do pagination in Fragment.

This approach needs time to change flow a bit. But is most robust way in your case.

Edit

If you are already using pagination and still getting large data at one time, that's again an issue. Because pagination is used to escape these memory issues.

You can do 2 things as solution. 1. Ask for limited data at once, say 50-60 items per request. 2. You can map and remove unnecessary fields from your list when passing in intent.

I would prefer the first one.

Khemraj Sharma
  • 57,232
  • 27
  • 203
  • 212
  • Thanks for the answer, but I think you didn't go through question properly as per your "Important" section in the answer. I'm already doing pagination "pagination_index". But once data is loaded in screen one with pagination. I have to pass existing data and last pagination index. But the problem is data is huge and not able to pass with intent. – Chitrang Nov 11 '18 at 02:44
  • What your data contains that it exceed 1mb in just 100 size of list? – Khemraj Sharma Nov 11 '18 at 03:58
0

I know I'm late but this will help some future visitor.

Add Pagination & transfer data with arraylist & clicked position to detail activity using intent & after, set the current position of clicked position. like viewpager.setCurrentPosition(clickedPosition);

Jeetu Kashyap
  • 21
  • 1
  • 4