简体   繁体   中英

Is there any method that i can call on SecondActivity to add a new item on Arraylist on FirstActivity.Fragment that has Recyclerview?

Hi im currently building my First android app and i have a problem that i cant solve this past 3 days. so i have MainActivity and on it there is a Fragment with Recyclerview and ArrayList< Item > listOfItem , on this Fragment there is a Floating button that when i click it will take me to SecondActivity there is a edit text on this SecondaryActivity that i have to fill then i will pass the data back to listOfItem .My problem is what method can i call on SecondActivity to create/add an listOfItem when i go back to MainActivity Fragment? I dont want to make Adapter and listOfItem to be static. Is there a way? thanks

Simple, You can achieve this by using Shared Preference.

What you need to do is just save that Edit Text data to Model(POJO), save that Model to Preference and in your Main Activity - Fragment on Resume method show updated data from that Preference.

Yeah here you need to take care of

The fragments onResume() or onPause() will be called only when the Activities onResume() or onPause() is called. They are tightly coupled to the Activity.

为了更方便和有效地处理从任何活动或片段到任何活动或片段的数据,您可以使用事件总线..这里是您将获得详细信息的链接: https : //github.com/greenrobot/EventBus

The best solution would be to start the second Activity for result.

startActivityForResult(ActivityBIntent)

Then checking the result overriding

onActivityResult() on the first activity and updating your list/adapter from it

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