TabFragments FragmentPageAdapter Parcelable Bundle - android

I'm developing a Xamarin Android application, I've got a main activity and a Tab Fragment that holds four fragments. From the MainActivity I sent a parcelable object to the Tab Fragment, through arguments (which as I said holds the 4 fragments).
The thing is I that this object needs to be sent to the fragment 1, this fragment sent the object to the Fragment 2 and so on. Each one of these fragments needs to change some different properties of the same object. It's like a wizard form, where each tab is a step.
How can I approach that? How can I send the object from fragment to the next fragment?
I hope I made it clear.
Thanks in advance.
Here are some lines of code:
//In the MainActivity
private void ListItemClicked(int position)
{
//this way we don't load twice, but you might want to modify this a bit.
if (position == oldPosition)
return;
oldPosition = position;
Android.Support.V4.App.Fragment fragment = null;
switch (position)
{
case 0:
fragment = FragmentHome.NewInstance();
break;
case 1:
sheet = new Hoja();
bundle = new Bundle();
pa = Parcel.Obtain();
bundle.PutParcelable("sheet", sheet);
pa.WriteBundle(bundle);
pa.SetDataPosition(0);
fragment = TabFragment.NewInstance();
fragment.Arguments = bundle;
break;
}
SupportFragmentManager.BeginTransaction()
.Replace(Resource.Id.content_frame, fragment)
.Commit();
}
//In the TabFragments class receive the object
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
//var ignored = base.OnCreateView(inflater, container, savedInstanceState);
parcelable_sheet = (Hoja)Arguments.GetParcelable(PARCEL_KEY);
}

Related

Persistant fragments and viewpager

I want to use 3 fragments within an Android App, I red:
Creating-and-Using-Fragments
.But I want to use the viewpager for swiping and displaying the fragments like explained in:
ViewPager-with-FragmentPagerAdapter
.But this code or the default code of Android Studio Example, use newInstance to create the instances, each time it is needed and destroy when not needed.
// Returns the fragment to display for that page
#Override
public Fragment getItem(int position) {
switch (position) {
case 0: // Fragment # 0 - This will show FirstFragment
return FirstFragment.newInstance(0, "Page # 1");
case 1: // Fragment # 0 - This will show FirstFragment different title
return FirstFragment.newInstance(1, "Page # 2");
case 2: // Fragment # 1 - This will show SecondFragment
return SecondFragment.newInstance(2, "Page # 3");
default:
return null;
}
}
but I want to create once for all :
// Within an activity
private FragmentA fragmentA;
private FragmentB fragmentB;
private FragmentC fragmentC;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
fragmentA = FragmentA.newInstance("foo");
fragmentB = FragmentB.newInstance("bar");
fragmentC = FragmentC.newInstance("baz");
}
}
and only hide/show them, as in the example:
// ...onCreate stays the same
// Replace the switch method
protected void displayFragmentA() {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
if (fragmentA.isAdded()) { // if the fragment is already in container
ft.show(fragmentA);
} else { // fragment needs to be added to frame container
ft.add(R.id.flContainer, fragmentA, "A");
}
// Hide fragment B
if (fragmentB.isAdded()) { ft.hide(fragmentB); }
// Hide fragment C
if (fragmentC.isAdded()) { ft.hide(fragmentC); }
// Commit changes
ft.commit();
}
But how to do that with a FragmentPagerAdapter
public Fragment getItem(int position) no longer has to be like that
Also, how to access a data
public double [][] tableaux;
that is in the MainActivity from one Fragment.
Data will be persistant if I assign a pointer of the Fragment I just created in the MainActivity onCreate to point on the MainActivity.tableaux
You can return you pre-initialized fragments in getItem method.
#Override
public Fragment getItem(int position) {
switch (position) {
case 0: return fragmentA;
case 1: return fragmentB;
case 2: return fragmentC;
default: return null;
}
UPDATE: #Alok is right. You should not have fragment references in Activity. And I suggest instead of increasing offscreen page limit using setOffscreenPageLimit, you should consider saving & restoring fragment states using public void onSaveInstanceState(Bundle) and savedInstanceState argument of public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState).
#Lotfi you don't need to save your fragment inside activity, it is prone to leaks .
Also you can assign id to each of your fragment and later when you need to reuse that fragment you can ask fragment manager to return the fragment by passing ie. by calling fragment manager method findFragmentByID() inside your getItem().

how to pass data from activity to swipable fragments?

i have data in activity in which some data i want to show in activity itself and some data i want to show in three swipable fragments. so how do i pass data from activity to that fragments. i am gonna put image of what i want to do.
Please give solution about this.Thanks.
I think your main question is how to pass data from activity to fragment?Right?
You can pass data from activity to fragment by creating a Bundle and put some data to it and then pass it to the setArguments method.
YourFragment yourFragment = new YourFragment();
Bundle bundle = new Bundle();
bundle.putInt(KEY_RECIPE_INDEX, index);//you can use bundle.putString or others to pass different types of data
yourFragment.setArguments(bundle);
Now you can retrive the data in your fragment by using getArguments
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
int index = getArguments().getInt(ViewPagerFragment.KEY_RECIPE_INDEX);
//........
You also ask in your question that you have some data in your activity and some of the data you want to show on activity and some of them you want to show in swipable fragments.Now I am going to show you how to do that by using an example.
Suppose you have two swipable tab and each tab has a fragment
final IngredientsFragment ingredientsFragment = new IngredientsFragment();
Bundle bundle = new Bundle();
bundle.putInt(KEY_RECIPE_INDEX, index);
ingredientsFragment.setArguments(bundle);
final DirectionsFragment directionsFragment = new DirectionsFragment();
bundle = new Bundle();
bundle.putInt(KEY_RECIPE_INDEX, index);
directionsFragment.setArguments(bundle);
ViewPager viewPager = (ViewPager) view.findViewById(R.id.viewPager);
viewPager.setAdapter(new FragmentPagerAdapter(getChildFragmentManager()) {
#Override
public android.support.v4.app.Fragment getItem(int position) {
return position == 0 ? ingredientsFragment : directionsFragment;
}
#Override
public CharSequence getPageTitle(int position) {
return position == 0 ? "Ingredients" : "Directions";
}
#Override
public int getCount() {
return 2;
}
});
TabLayout tabLayout = (TabLayout) view.findViewById(R.id.tabLayout);
tabLayout.setupWithViewPager(viewPager);
I think there is no problem of the data that you want to show in your activity.
Qick recap: You pass the data to fragment by creating a bundle and put some data and use yourfragment.setArguments(yourBundle).
Now I think you can pass data to swipable views.
Hope this helps!

how to reload calling fragment with the previous state and set the result got as a callback from the dialog fragment it initiated

This is how my application runs.
The application has several fragments as view pager connecting through main activity. From Second Fragment, I launched custom fragment dialog and upon ok button click inside that fragment dialog, I am fetching the result into main activity. I am using android.support.v4 library.
public class MainActivity extends FragmentActivity implements MyDialog.Communicator {
private static String callBackMessage;
#Override
public Fragment getItem(int pos) {
switch (pos) {
case 0:
return FirstFragment.newInstance();
case 1:
return SecondFragment.newInstance(MainActivity.this);
default:
return ThirdFragment.newInstance();
}
}
#Override
public void onDialogMessage(String callBackMessage) {
MainActivity.callBackMessage = callBackMessage;
}
public static String getCallBackMessage() {
return callBackMessage;
}
This is my MyDialog Fragment class, here I am creating some rows and giving checkboxes to be selected.
private void performOKButtonFunctionality() {
String msg = checkedBoxesCount + "";
communicator.onDialogMessage(msg);
SecondFragment secondFragment = new SecondFragment();
secondFragment.testFunction(row);
dismiss();
}
Now because of my very early days into android and its fragment life cycle, I am calling a method on Second fragment after instantiating it. the row parameter just passes the row number.
Now inside the Second Fragment Class I have some thing like,
public void testFunction(String callBackRow) {
callBackRowNo = Integer.parseInt(callBackRow);
String callBackMessage = MainActivity.getCallBackMessage();
getTableData(callBackMessage + "");
}
In order to save the SecondFragment previous state, I am saving its context by making it static like this,
private static Context activityContext;
public static SecondFragment newInstance(Context context) {
activityContext = context;
SecondFragment f = new SecondFragment();
Bundle b = new Bundle();
f.setArguments(b);
return f;
}
Now doing so, I am able to use my Second Fragment only one time after clicking the ok button from Fragment Dialog. but upon clicking again, the app crashes at this point.
FragmentManager myDialogManager = getActivity().getSupportFragmentManager();
The raised exception is:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.support.v4.app.FragmentManager android.support.v4.app.FragmentActivity.getSupportFragmentManager()' on a null object reference
Now, my questions are:
How do we reenter into the oncreate method of the fragment where it just restores its previous state rather than saving it explicitly before launching
fragment dialog.
How does the fragment dialog pass parameter to the fragment which can be used to update the fragment field.
Is there some easy way to achieve this?
Before asking I searched the SO but could not find anything where this scenario was asked or explained.
I am badly stuck here and any solution/direction is much appreciated.
try getChildFragmentManager() instead of getSupportFragmentManager()
when do you make fragment with newInstace, you can put argsments like this.
public static MyFragment newInstance(int value) {
MyFragment f = new MyFragment();
Bundle args = new Bundle();
args.putInt("value", value);
f.setArguments(args);
return f;
}
it also needs constructor MyFragment()
public MyFragment() {}
get values from args like this
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (getArguments() != null) {
value = getArguments().getInt("value");
}
return inflater.inflate(R.layout.your_layout, container, false);
}

How do I use the same fragment for three tabs with different content?

I have an enum describing three different sports:
public enum MatchType {
S1(0, "Sport1", "xml stream address", R.id.match_list, R.layout.fragment_match_list, R.color.separator_sport1),
S2(0, "Sport2", "xml stream address", R.id.match_list, R.layout.fragment_match_list, R.color.separator_sport2),
S3(0, "Sport3", "xml stream address", R.id.match_list, R.layout.fragment_match_list, R.color.separator_sport3);
...getters/setters
}
I then have fragment with
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
matchesArrayAdapter = new MatchListAdapter(getActivity(), new ArrayList<Match>());
return inflater.inflate(matchType.getLayout(), container, false);
}
Also in my fragment I have an AsyncTask where I have this
#Override
protected void onPostExecute(final List<Match> matches) {
if (matches != null) {
matchListView = (ListView) getActivity().findViewById(matchType.getRId());
[setup listeners]
matchesArrayAdapter.matchArrayList = matches;
matchListView.setAdapter(matchesArrayAdapter);
}
}
EDIT:
In my Activity I have an AppSectionsPagerAdapter with
public Fragment getItem(int i) {
MatchListSectionFragment fragment = new MatchListSectionFragment();
Bundle bundle = new Bundle();
bundle.putInt(Constants.MATCH_TYPE, i);
fragment.setArguments(bundle);
return fragment;
}
EDIT 2:
Here's my onCreate and onCreateView from my fragment:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = getArguments();
matchType = MatchType.getMatchType(bundle.getInt(Constants.MATCH_TYPE));
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
matchesArrayAdapter = new MatchListAdapter(getActivity(), new ArrayList<Match>());
return inflater.inflate(matchType.getLayout(), container, false);
}
The AsyncTask reads an xml stream for each of the sports in my enum but my problem is that tab #1 is overwritten with data from tab #2 and subsequently tab #3.
Before I had a fragment defined for each sport but surely that can't be necessary?
How do I go about using the same fragment with the same layout for each sport?
When instantiating your fragment in your Activity set the Fragment's arguments with a Bundle.
Bundle myBundle = new Bundle();
myBundle.putInt(MY_EXTRA, 1);
myFragment.setArguments(myBundle);
In your bundle put some Extra that will be read in the fragment's onCreate() callback.
int i = getArguments().getInt(MY_EXTRA);
I am on mobile.
Put three FrameLayouts in a LinearLayout, named frame1, frame2 and frame 3. This will be your main Activity's layout.
Then in the Activity's oncreate() method, call getFragmentManager().getFragmentTransaction().
Instantiate the three fragments and send them the data, preferably through a Bundle.
On the Fragment Transaction call the add() or replace() method for each fragment, the first parameter is the id of the respective FrameLayout, the second parameter is the fragment itself.
Call commit().
You should create the newInstance method in your fragment, also you should store MatchType instansce in you fragment.
MatchType matchType;
public static MyFragment newInstance(MatchType matchType) {
MyFragment fragment = new MyFragment();
fragment.matchType = matchType;
return fragment;
}
In your Activity you should to create 3 instances of MyFragment with this method (with related to each fragment it owns MatchType). Then in onCreateView method you should insert data to your views from matchType.
Sorry, I'm on mobile. And sorry for my English.
Update
Check your variable matchType. Maybe it declared as static?

How can i create a UI for each tab of a tabbed activity?

I'm learning developing for android but i don't know how to create the UI for a Fragment.
I created a new activity and during the creation process I selected the navigation type "Tabs + Swipe".
Now i have a layout xml which i can't modify using the WYSIWYG interface and if i -for example- create a button widget using java in the class file it creates it in every "tab view".
I basically want to create different interfaces for every tab (fragment).
Thank you
In just created Activity you can find inner class SectionsPagerAdapter. Look at this method:
#Override
public Fragment getItem(int i) {
Fragment fragment = new DummySectionFragment();
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, i + 1);
fragment.setArguments(args);
return fragment;
}
This method for every tab returns instance of DummySectionFragment with only different bundles. If you want to create fragments with different views for every tab you should check value of i variable and according to this value create proper fragment. For example:
#Override
public Fragment getItem(int i) {
Fragment fragment;
switch(i){
case 0:
fragment = new MyFragment1();
break;
case 1:
fragment = new MyFragment2();
break;
case 3:
fragment = new MyFragment3();
break;
default:
throw new IllegalArgumentException("Invalid section number");
}
//set args if necessary
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, i + 1);
fragment.setArguments(args);
return fragment;
}
Instead of DummySectionFragment class create three classes: MyFragment1, MyFragment2, MyFragment2 and for each, inside method onCreateView create or inflate view, for example:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.my_fragment1.xml, null);
return v;
}
Where R.layout.my_fragment1.xml is layout of your MyFragment1 fragment.

Categories

Resources