简体   繁体   中英

How to share common resources between fragments?

I have an Activity with a ViewPager, I have 3 tabs on it. The app starts with the EventFragment, this is the first tab, it has items that can be checked in a checkbox. I want to get the items that are checked in EventFragment, when I switch to TicketFragment, which is the 2nd tab.

My initial idea is to make PagerActivity Singleton and make it store all the datas, and it can be requested from there, but I don't know if it's a good approach.

My code:

PagerActivity (Launcher)

public class PagerActivity extends AppCompatActivity {

    public Activity activity = this;
    private static final String TAG = "PagerActivity";
    private BettingAdapter adapter;
    private BettingListDatabase database;
    private Answer bettingData=null;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pager);
    }

    @Override
    protected void onResume() {
        super.onResume();
        ViewPager mainViewPager = findViewById(R.id.mainViewPager);
        TabPagerAdapter tabPagerAdapter = new TabPagerAdapter(getSupportFragmentManager(), this);
        mainViewPager.setAdapter(tabPagerAdapter);
    }
}

PagerAdapter

public class TabPagerAdapter extends FragmentPagerAdapter {
    private Context context;

    public TabPagerAdapter(FragmentManager fm, Context context) {
        super(fm);
        this.context = context;
    }

    // This method is called whenever the adapter needs a Fragment for a certain position
    @Override
    public Fragment getItem(int position) {
        Fragment ret = null;
        switch (position) {
            case 0:
                ret = new EventFragment();
                break;
            case 1:
                ret = new TicketFragment();
                break;

            case 2:
                ret = new BalanceFragment();
                break;
        }
        return ret;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        String title;
        switch (position) {
            case 0:
                title = context.getString(R.string.events);
                break;
            case 1:
                title = context.getString(R.string.ticket);
                break;
            case 2:
                title = context.getString(R.string.balance);
                break;
            default:
                title = "";
        }
        return title;
    }

    @Override
    public int getCount() {
        return 3;
    }
}

EventFragment: pastebin

TicketFragment:

public class TicketFragment extends Fragment {

    private BettingAdapter adapter;
    private RecyclerView recyclerView;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.content_ticket,
                container, false);

        recyclerView = view.findViewById(R.id.MainRecyclerView);
        adapter=new BettingAdapter((BettingAdapter.BettingItemClickListener) getActivity().getParent());

        return view;
    }
}

There is a lot of options.
1. Access activity from EventFragment, store data here, read it from TicketFragment.
2. Store data at preferences or database, more about saving data: https://developer.android.com/training/data-storage/shared-preferences
3. Create VM for this shared state, more about VMs: https://developer.android.com/topic/libraries/architecture/viewmodel
4. Singleton is acceptable, if it's new class for data, activity can't be singleton, lifecycle of activity handled by android, but VM is evolution of this approach.

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