How to restart activity with data of previous or next position of a ListView

[postlink] https://the-best-way-of-life-is-islam.blogspot.com/2015/07/how-to-restart-activity-with-data-of.html [/postlink]

I'm building a book reader app and currently having some issues on how to move to next or previous chapter.

The fragment that is in charge of showing a list of chapter (basically a ListView). I capture the item with onItemClick:

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            Intent intent = new Intent(getActivity().getApplicationContext(),ReaderActivity.class);
            intent.putExtra("name",adapter.getItem(i).getName());
            intent.putExtra("url",adapter.getItem(i).getUrl());
            startActivity(intent);
        }
});

It will start the ReaderActivity and display the content of the book. Now the ReaderActivity has 2 buttons for moving to next/previous chapter. How can I get the data (the name and url) from the ListView?

At the moment I'm thinking of two methods:

  1. Calling finish() on ReaderActivity and override the onDestroy() method to call a method to get the next/previous data in the fragment that holds the ListView.

  2. Passing the whole ArrayList of chapters to ReaderActivity so it can retrieve the item it wants (probably not a good solution since there are too many chapters).

Is there a better way?

اذا أعجبك الموضوع ساهم في نشره :

Enregistrer un commentaire

 
حقوق النشر: Published by | Template Created by | القناة على اليوتيب
copyright © 2014. The Best Way Of Life Is Islam -يمكن نقل كل ما هو موجود في موقع الرحمة تيوب
القرآن الكريم و الأحاديث النبوية أناشيد و دروس اسلامية منوعة موقع الفيديوهات الاسلامية
تطوير و تعريب مدونة ثعلوب