如何在片段之间共享公共资源?

时间:2019-11-27 10:30:18

标签: java android fragment

我有一个带有ViewPager的活动,上面有3个标签。该应用程序从EventFragment开始,这是第一个选项卡,它具有可在复选框中选中的项目。当我切换到第二个选项卡的TicketFragment时,我想获取在EventFragment中检查的项目。

我最初的想法是制作PagerActivity Singleton并使其存储所有数据,可以从那里请求它,但是我不知道这是一个好方法。

我的代码:

PagerActivity(启动器)

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

1 个答案:

答案 0 :(得分:1)

有很多选择。
1.从EventFragment访问活动,将数据存储在此处,然后从TicketFragment中读取。
2.将数据存储在首选项或数据库中,有关保存数据的更多信息:https://developer.android.com/training/data-storage/shared-preferences
3.为此共享状态创建VM,有关VM的更多信息:https://developer.android.com/topic/libraries/architecture/viewmodel
4. Singleton是可以接受的,如果它是数据的新类,则活动不能是singleton,Android会处理活动的生命周期,但是VM是这种方法的演变。