How to share common resources between fragments? - java

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.

Related

when moving from one fragment to other the elements of the former staying on screen

so i got one main activity that has a Tablayout and a ViewPager to present diffrent fragments.
when i move between the fragments with my Tablayout everything works good, but if i use a button to open fragment, when going back to the former fragment (by pushing the cancel button) the elements of the fragment i left staying on the screen (as in the picture).
i tried to use a method viewPager.setCurrentItem(0); in the fragment to go back to the homepage from the fragment instade of ft.replace(R.id.fragment_edit_reminder, new Main_Activity_fragment()).commit(); but it didn't moved back to my home fragment (with the repalce it does go back but as i saied the elements.
this is my main:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TabLayout tableLayout = findViewById(R.id.Tablayouting);
final ViewPager viewPager = findViewById(R.id.ViewPager);
PagerAdapter pagerAdapter = new
PagerAdapter(getSupportFragmentManager(),tableLayout.getTabCount());
viewPager.setAdapter(pagerAdapter);
tableLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
}
this is the fragment:
public class edit_reminder_fragment extends Fragment implements View.OnClickListener {
private Button cancelButton;
public edit_reminder_fragment() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* #return A new instance of fragment edit_reminder_fragment.
*/
public static edit_reminder_fragment newInstance() {
edit_reminder_fragment fragment = new edit_reminder_fragment();
Bundle args = new Bundle();
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
}
}
#RequiresApi(api = Build.VERSION_CODES.O)
public void onClick(View view)//TODO: make a utility method for switching fragments on the main_activity_fragment(see note).
{
switch (view.getId()) {//recognizing what button was pushed
case R.id.ButtonCancelReminder:
//region
FragmentTransaction ft = getFragmentManager().beginTransaction();
Main_Activity_fragment maf = new Main_Activity_fragment();
ft.replace(R.id.fragment_edit_reminder, maf).commit();
break;
//endregion
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_edit_reminder_fragment, container, false);
cancelButton = view.findViewById(R.id.ButtonCancelReminder);
cancelButton.setOnClickListener(this);
return view;
}
}
my pagerAdapted class:
public class PagerAdapter extends FragmentPagerAdapter {
//https://www.youtube.com/watch?v=HHd-Fa3DCng&ab_channel=MasterCoding
private int numOfTabs;
public PagerAdapter(FragmentManager fm, int numOfTabs) {
super(fm);
this.numOfTabs = numOfTabs;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new Main_Activity_fragment();
case 1:
return new key_words_fragment();
case 2:
return new groups_and_points_fragment();
case 3:
return new Fragment_Past_Reminders();
case 4:
return new edit_reminder_fragment();
default:
return null;
}
}
#Override
public int getCount() {
return numOfTabs;
}
}
in the picture we see the home page fragment ,on it 3 buttons (cancel, save, Add a sub reminder) that stayed from a fragment that open when clicking on the ADD NEW REMINDER button (when clicking on cancel in the second fragment it's going back to the home page):
First, try the following as an adaper:
public class PagerAdapter extends
FragmentPagerAdapter {
private final ArrayList<Fragment> mFragments = new ArrayList<>();
private final ArrayList<String> mFragmentTitle = new ArrayList<>();
public PagerAdapter(FragmentManager mManager) {
super(mManager);
}
#Override
public Fragment getItem(int position) {
return mFragments.get(position);
}
#Override
public int getCount() {
return mFragments.size();
}
public void addFragment(Fragment fragment, String title) {
mFragments.add(fragment);
mFragmentTitle.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitle.get(position);
}
public Fragment getFragment(int position) {
if (position < mFragments.size() && position >= 0) {
return mFragments.get(position);
}
return null;
}
}
Then in your MainActivity's onCreate, initialize the following:
mTabs = (TabLayout) findViewById(R.id.tabs);
mPager = (ViewPager) findViewById(R.id.view_pager);
mMainAdapter = new PagerAdapter( getSupportFragmentManager() );
setupContents();
Then the private method setupContents() as follows:
mMainAdapter.addFragment(new FragmentOne(), getResources().getString(R.string.tab_albums));
mMainAdapter.addFragment(new FragmentTwo(), getResources().getString(R.string.tab_songs));
mMainAdapter.addFragment(new FragmentThree(), getResources().getString(R.string.tab_playlists));
// you can add as many fragments as you wish
//just follow the previous method #mMainAdapter.addFragment
mPager.setAdapter(mMainAdapter);
mTabs.setupWithViewPager(mPager);
To get the current fragment:
public Fragment getCurrentFragment() {
return mMainAdapter.getFragment(mPager.getCurrentItem());
}
To check if the current fragment is FragmentOne:
private boolean isFragmentOne() {
return getCurrentFragment() instanceof FragmentOne;
}

send intent with extrastring from activity to tab fragment

My app's activity hierarchy is:
MainActivity > FragActivity > FragmentExp(It includes 3 tab fragment(Tab1 Tab2 Tab3))
I have recycler view in MainActivity and when clicked in different list I want to go to Tabs with extrastrings(I mean some string message). And, show toast in those tabs.
What I've tried is:
I set intent with extrastring from my MainActivity to FragActivity like this.
if (position==1){
Intent i = new Intent(MainActivity.this, FragActivity.class);
i.putExtra(MainActivity.ENGLISHPDF, "0");
startActivity(i);
}
and get those strings directly in Tab1 and Tab2 like this:
In Tab1
code = getActivity().getIntent().getStringExtra(MainActivity.ENGLISHPDF);
if (this.code.equals("0")) {
Toast.makeText(getContext(), "hey its 1", Toast.LENGTH_SHORT).show();
}
In Tab2
code = getActivity().getIntent().getStringExtra(MainActivity.ENGLISHPDF);
if (this.code.equals("0")) {
Toast.makeText(getContext(), "hey its 2", Toast.LENGTH_SHORT).show();
}
These are my codes:
MainActivity:
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, DataAdapter.OnNoteListener{
public static final String ENGLISHPDF = "english";
RecyclerView recyclerView;
#Override
protected void onCreate(Bundle savedInstanceState) {
(...)
recyclerView = findViewById(R.id.add_header);
(...)
ArrayList<ListItem> items = new ArrayList<>();
items.add(new Item(R.drawable.green, "English", "1"));
DataAdapter adapter = new DataAdapter(items, this);
recyclerView.setAdapter(adapter);
}
#Override
public void onNoteClick(int position) {
if (position==0){
Intent i = new Intent(MainActivity.this, FragActivity.class);
i.putExtra(MainActivity.ENGLISHPDF, "0");
startActivity(i);
}
}
}
FragActivity:
public class FragActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_frag);
getSupportFragmentManager().beginTransaction().add(R.id.fragment_container,
new FragmentExplorem(), COLLAPSING_TOOLBAR_FRAGMENT_TAG).commit();
}
And finally in FragExp I've set tablayout and include 3 tabs fragment in it.
This is FragExp:
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.tab_layout, container, false);
AppBarLayout appBarLayout = view.findViewById(R.id.tab_appbar_layout);
((CoordinatorLayout.LayoutParams) appBarLayout.getLayoutParams()).setBehavior(new AppBarLayoutBehavior());
tabLayout = view.findViewById(R.id.tabs);
tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
tabLayout.setTabGravity(TabLayout.GRAVITY_CENTER);
viewPager = view.findViewById(R.id.viewpager);
viewPager.setOffscreenPageLimit(tab_count);
toolbar = view.findViewById(R.id.toolbar);
setupToolbar();
viewPager.setAdapter(new MyAdapter(getChildFragmentManager()));
tabLayout.post(new Runnable() {
#Override
public void run() {
tabLayout.setupWithViewPager(viewPager);
viewPager.setCurrentItem(0);
}
});
return view;
}
public class MyAdapter extends FragmentPagerAdapter {
private MyAdapter(FragmentManager fm) {
super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0: return new Tab1();
case 1: return new Tab2();
case 2: return new Tab3();
}
return null;
}
#Override
public int getCount() {
return tab_count;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0 : {
return "First";
}
case 1 : {
return "Second";
}
case 2 : {
return "Third";
}
return null;
}
}
Get fragment instance like following function.
This way of getting instance ensures all the clients need the
instance have the same process of constructing the Fragment.
public class Tab1 extends Fragment {
private static final String KEY_CODE = "key_code";
public static Fragment newInstance(#Nullable String code) {
Bundle args = new Bundle(1);
if (code!= null)
args.putString(KEY_CODE, code);
Tab1 fragment = new Tab1();
fragment.setArguments(args);
return fragment;
}
}
when you need Tab1 instance, you should pass the parameter or null at the same time.
#Override
public Fragment getItem(int position) {
switch (position) {
case 0: return Tab1.newInstance(code);
case 1: return Tab2.newInstance(code);
case 2: return Tab3.newInstance(code);
}
return null;
}
Then you can use the code by invoking Fragment.getArguments().
// In Tab1
private String mCode;
private String DEFAULT_CODE = "0";
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mCode = getArguments().getString(KEY_CODE , DEFAULT_CODE);
// Now you can use the mCode in any place in Tab1 you want, but make sure you do Null check before using it
}
BTW, if the "code" is dynamic, then it will be totally different.

Trying to send the userid from the activity to the viewholder fragment

I have created an android app in which I have added an activity called profileview.java on which users can view there or other user's profile data. Profileview activity contains two fragments post and likes on which I have to show the post that is been posted by the user and the post liked by the user but not getting any idea how to send the userid from the profileview activity to the other two fragments.
profileviewactivity.class
public class profileviewActivity extends AppCompatActivity {
public String userid;
private ViewPager viewPager;
private TabLayout tabLayout;
private settingtabAdopter Tabadapter;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profileview);
viewPager = (ViewPager) findViewById(R.id.viewpager);
Tabadapter = new settingtabAdopter(getSupportFragmentManager());
viewPager.setAdapter(Tabadapter);
tabLayout = (TabLayout) findViewById(R.id.tablayout);
tabLayout.setupWithViewPager(viewPager);
userid = getIntent().getExtras().get("userid").toString();
}
}
settingadapter.java
public class settingtabAdopter extends FragmentPagerAdapter {
public settingtabAdopter(#NonNull FragmentManager fm) {
super(fm);
}
#NonNull
#Override
public Fragment getItem(int position) {
switch (position){
case 0:
posttimeline postfragment = new posttimeline();
return postfragment;
case 1:
like chatfragment = new like();
return chatfragment;
default:
return null;
}
}
#Override
public int getCount() {
return 2;
}
#Nullable
#Override
public CharSequence getPageTitle(int i) {
switch (i){
case 0:
return "Posts";
case 1:
return "Likes";
default:
return null;
}
}
}
posttimeline.java
public class posttimeline extends Fragment {
View posttimelineview;
public posttimeline() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
posttimelineview = inflater.inflate(R.layout.fragment_posttimeline, container, false);
return posttimelineview;
}
}
like.java
public class like extends Fragment {
View Likeview;
public like() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
Likeview = inflater.inflate(R.layout.fragment_like, container, false);
return Likeview;
}
}
i want to send userid from the profileaviewactivity to the posttimeline and like fragment.
pass userid as parameter at settingtabAdopter constructor
public settingtabAdopter(#NonNull FragmentManager fm, String userid)
profileviewActivity -> onCreate
Tabadapter = new settingtabAdopter(getSupportFragmentManager(), userid);
make private variable inside settingtabAdopter
private String userid;
initialize it inside constructor
this.userid = userid
Create private variable in each fragment and add setter (posttimeline and like) fragments
private String mUserid;
void setMUserId(String mUserid){
this.mUserid = mUserid;
}
On settingtabAdopter inside getItem function after initialize each fragment set the private varaible with the userid
public Fragment getItem(int position) {
switch (position){
case 0:
posttimeline postfragment = new posttimeline();
postfragment.setMUserId(userid)
return postfragment;
case 1:
like chatfragment = new like();
chatfragment.setMUserId(userid)
return chatfragment;
default:
return null;
}
}

How To Hide View on Specific Fragment

I want to hide a GridView by setting drawerGrid.setVisibility(View.GONE); when the ViewPager is on a specific layout. Do I need to implement a onPageListener or is there an easier option?
Launcher.java
public class Launcher extends FragmentActivity {
DrawerAdapter drawerAdapterObject;
GridView drawerGrid;
class Package {
Drawable icon;
String name;
String label;
}
Package[] packs;
PackageManager pm;
ViewPager viewpager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_launcher);
drawerGrid = (GridView) findViewById(R.id.content);
pm=getPackageManager();
set_packs();
drawerAdapterObject = new DrawerAdapter(this, packs);
drawerGrid.setAdapter(drawerAdapterObject);
viewpager = (ViewPager) findViewById(R.id.pager);
PagerAdapter adapter = new PagerAdapter(getSupportFragmentManager());
viewpager.setAdapter(adapter);
viewpager.setCurrentItem(1);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Window w = getWindow();
w.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
}
}
public void set_packs() {
final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> packsList = pm.queryIntentActivities(mainIntent, 0);
packs = new Package[packsList.size()];
for(int I=0;I<packsList.size();I++){
packs[I]= new Package();
packs[I].icon=packsList.get(I).loadIcon(pm);
packs[I].name=packsList.get(I).activityInfo.packageName;
packs[I].label=packsList.get(I).loadLabel(pm).toString();
}
}
PagerAdapter.java
public class PagerAdapter extends FragmentPagerAdapter {
public PagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int arg0) {
switch (arg0) {
case 0:
return new Fragment1();
case 1:
return new Fragment2();
default:
break;
}
return null;
}
#Override
public int getCount() {
return 2;
}
}
Fragment1.java
public class Fragment1 extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.activity_apps,container,false);
}
}
Fragment2.java
public class Fragment2 extends Fragment {
ViewPager viewpager;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.activity_homescreen,container,false);
}
}
You can do that in two ways:
As you mentioned by using addOnPageChangedListener
Using callback when your fragment become visible to user
How to determine when Fragment becomes visible in ViewPager:
try this;
public class MyFragment extends Fragment {
#Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser) {
//hide view here
}
else {
}
}
}
setUserVisibleHint
Set a hint to the system about whether this fragment's UI is currently visible to the user. This hint defaults to true and is persistent across fragment instance state save and restore.
here isVisibleToUser boolean: returns true if this fragment's UI is currently visible to the user (default), false if it is not.

How to load data into a FragmentActivity to be used by nested fragments?

I have a FragmentActivity with two Fragments. The two Fragments depend on a shared object. The object is loaded by an AsyncTask. So the problem here is that when the activity creates and shows up the fragments, the object is not loaded yet. Is there any method that pauses the Fragment creation or something like this?
The scenario is like this:
FragmentActivity
public class MainActivity extends FragmentActivity {
private Object sharedObject;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new FooAsyncTask(...).execute();
}
...
public void onAsyncTaskCompleted(result) {
sharedObject = result;
}
// Pager adapter -------------------------------------------------------
private class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch(position) {
case 0:
return FooFragment.newInstance(sharedObject);
case 1:
return BarFragment.newInstance(sharedObject);
default:
throw new IllegalStateException();
}
}
#Override
public String getPageTitle(int position) {
switch(position) {
case 0:
return "Foo";
case 1:
return "Bar";
default:
throw new IllegalStateException();
}
}
#Override
public int getCount() {
return 2;
}
}
}
FooFragment
public class FooFragment extends Fragment {
public FooFragment newInstance(Object sharedObject) {
FooFragment f = new FooFragment();
f.setObject(sharedObject);
return f;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
}
...
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
...
// Here I use the object to show data
}
}
BarFragment
public class BarFragment extends Fragment {
public BarFragment newInstance(Object sharedObject) {
BarFragment f = new BarFragment();
f.setObject(sharedObject);
return f;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
}
...
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
...
// Here I use the object to show data
}
}
Final thoughts
So, as you can see, I need the same data for the two fragments, and this data needs to be loaded in an AsyncTask. The ideal behavior for me would be:
Load MainActivity
Show ProgressDialog and load the data in the background
Dismiss ProgressDialog
Show Fragments and use the data
You do this wrong. You should NOT pause anything. Instead, when parent activity load what is needed it should tell the fragments about that. In fact I'd make it differently - by using listeners - so my fragment would need to register on creation and the object that loads my data would then broadcast message back once loading task it done.

Categories