J
J
jeezic2015-12-30 22:53:07
Android
jeezic, 2015-12-30 22:53:07

How to properly split an application into an Activity and a Fragment?

there is an application with one activity with NavigationDrawer. when you select a section in the NavigationDrawer, the corresponding fragment is loaded in the activity.
From one of the fragments, you need to call DatePicker (selecting a date from the calendar). The calendar is big enough, ie. to display it, you will either have to load a new fragment, or an activity in which the calendar will be displayed.
The selected date from the calendar should return to the original fragment.
Question: so all of them call a new activity and get the date from it, or send a call to the root activity, which will replace the fragment with a fragment with a calendar, process the return from the fragment with the calendar, return the fragment that called the request to the calendar?
I hope you can understand what the problem is tormenting me. You need the "right" approach or direction "where to read" about the organization.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
S
StanKo, 2015-01-03
@StanKo

In general, it is up to you to do it at your own discretion.
You can make fragments with a stack, but the side menu will still be available and the user will be able to go there and the logic of the app will break, i.e. Don't forget to turn off the side menu. However, with the system of fragments in the stack, there is a problem of saving this stack when leaving for the background and returning (launched the calendar, pressed Home like by accident, or you just suddenly got a call at that moment, returned to the app - rake) or when changing the orientation of the device from portrait to album (turned the device - a rake), because only the last fragment remains on the stack in such cases.
Accordingly, I want to go to the activity and in this case it is better to launch the activity on the result (startActivityForResult). Resp. there will be no side menu at all, and the stack will be saved normally. But here's the problem - the fragment does not have the onActivityResult () method, the fragment-parent activity has it, i.e. you will have to build crutches here for you to transfer this result from the activity to the fragment. Well, Google will tell you about this topic.
Well, or do as you yourself suggest - by replacing the fragments, 1 fragment is saved on the stack. But then again - how will you transport the target data in the end? Through SharedPreferences? Through the database? And how will you know that this is up-to-date data, and not left from the last choice?

A
Alexey, 2015-01-08
@Xapaxuc

In my humble opinion, it is too inconvenient to use the described approach to transfer data from a fragment to an activity, etc.
I would recommend looking at the https://github.com/greenrobot/EventBus library. It will solve the problem of transferring data from a fragment with a date selection to an Activity, where, as I understand it, this data is needed.
This option may seem even more convenient, given how often you have to deal with this kind of task when working with the GUI.

O
one pavel, 2015-12-31
@onepavel

You put the fragment with the DatePicker on top of the fragment where the result should be returned
using the add method in FragmentTransaction. You sign one fragment for the second
and catch the date

J
jeezic, 2015-01-04
@jeezic

In general, I placed the DatePicker in the DialogFragment, i.e. it is now displayed blocking all activity, respectively, and the menu on the left is also blocked.
It turned out that OnActivityResult can still be defined on a Fragment and called from another fragment. I was able to call a fragment with a DatePicker from my fragment, passing parameters to it through arguments, returning the result back to the Intent.
Calling the calendar was done like this:

DatepickerFragment newFragmentStartDate = 
           DatepickerFragment.newInstance(mYearDate, mMonthDate, mDayDate);
newFragmentStartDate.setTargetFragment(CostEditFragment.this, REQUEST_EDITDATE);
newFragmentStartDate.show(getActivity().getFragmentManager(), "datePicker");

"OK" button in the calendar with the following code:
Intent i = new Intent();
i.putExtra(EXTRA_YEAR, mYear);
i.putExtra(EXTRA_MONTH, mMonth);
i.putExtra(EXTRA_DAY, mDay);
getTargetFragment()
           .onActivityResult(getTargetRequestCode(), Activity.RESULT_OK, i);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question