Activities to Fragments - java

I have 3 activities which I want to convert into fragments so that I can implement a ViewPager to swipe between them:
Activity A ( to the left )
Activity B (central)
Activity C (to the right)
Activity B is called from an external activity and data is passed to it within the Intent. Now Activity B passes data within intents to Activities A and C when they are launched.The idea here is that B displays a slideshow with an ImageView which is updated every X seconds and A and C display information about the individual pictures. Hence, here are my questions:
How do I replicate this functionality with fragments so that when I swipe from Fragment B to C and from Fragment B to A I can pass the appropriate data to them. I currently do so this way with my activities:
Intent a = new Intent(SlideShow.this,MapsActivity.class);
a.putExtra("long", longitude);
a.putExtra("lang", langitude);
pauseSlideShow(i, timeRemaining);
isPaused = true;
startActivity(a)
I read up on the differences between FragmentPagerAdapter and FragmentStatePagerAdapter. Currently, when I launch activities A and C from B I do not call finish() on B so that when I return to it, it is in the exact same state as before. However, since activities A and C are supporting B, when I go back to activity B from A or C I end them by calling finish() on A and C respectively. How can I do this with fragments?

If you are using FragmentPagerAdapter, make sure that when you create a fragment in getItem() you save a reference to the fragment somewhere in the adapter or (preferably) the activity.
Create a method (i.e. getLatLong()) on fragment B that returns the data.
In fragment A in onCreateView(), you would get the reference to fragment B from the activity and call getLatLong() to get the data you need.
You can keep your fragments available in memory by calling viewpager.setOffscreenPageLimit(2). This way fragment A won't be destroyed when you swipe to fragment C, because the ViewPager will keep two fragments to the left of fragment C, which are your fragments A and B.
Another (probably better) approach is to create all three fragments at the beginning of the activity and return the appropriate fragment when getItem() is called. By doing this, you can use setTargetFragment() and getTargetFragment() to allow the fragments to get data from one another. When you do this, remember that a fragment may or may not have a UI available, so if you are pushing data rather than pulling, make sure the method receiving the data checks for getView() == null.

Related

How to prevent navigated from Activity from starting over in lifecycle and also how to have multiple "goback" activities?

1) When I click an item in a RecyclerView in Activity A, it takes me to another Activity B, when I navigate back from that Activity B back to A, A somehow calls onStart/onCreate and the RecyclerView data that I was scrolled at all changes. How to prevent this?
2) Say I have an Activity A and Activity B where I have buttons in both that navigate to Activity C. When I go back from Activity C, I want to determine which Activity I came from, A or B, to navigate back to. In AndroidManifest, I can only put ONE ParentActivity, so I don't know how to go about doing this.
onStart() will be called when resume activity A from B. You have to save the last scroll position of recyclerview yourself. you can refer to this answer
Not sure how you implement the navigation to activity C. If you use startActivityForResult() in A and B then you can override onActivityResult() in A and B to get the data from C
1) When I click an item in a RecyclerView in Activity A, it takes me
to another Activity B, when I navigate back from that Activity B back
to A, A somehow calls onStart/onCreate and the RecyclerView data that
I was scrolled at all changes. How to prevent this?
Instead of startActivity() start Activity B using startActivityForResult(). You can refer to this SO to implement it.
when I navigate back from that Activity B back to A, A somehow calls onStart/onCreate
onStart() will be called because Activity A entered onStop() after launching Activity B using startActivity()
onCreate() might be called if OS is under memory pressure and destroys your activity to relinquish some memory. It will recreate it before its being displayed back to user.
Refer to Activity lifecycle document.
Say I have an Activity A and Activity B where I have buttons in both
that navigate to Activity C. When I go back from Activity C, I want to
determine which Activity I came from, A or B, to navigate back to.
You can use startActivityForResult() here too.
Note: You need to be aware of potential issue with onStartActivity() getting called pre-maturely. Refer to this SO

Communication between fragments that belong to two different activities

I'm very new to Android development.
I've made some researches to achieve passing data from Fragment A in Activity A to Fragment B in Activity B.
Is that possible?
All I could get from the tutorials on the internet is that we have to create an Interface that will be implemented by the parent Activity of two sibling fragments. How to achieve it when the fragments belong to two different activities? I'm stuck.
Thanks
You would achieve it by implementing the said Interface and put the extras into the Intent for the Activity that has the other Fragment.
Pseudo code could look like this
Fragment A - calls interface methods on Activity A and passes data
Activity A - calls startActivity with the extras inside the Intent
Activity B - got started and evaluates the said Intent and passes it to Fragment B
Fragment B - receives the extras from Activity B
you can use Intent to pass data between activity and from sibling fragment call method of activity like ((MainActivity).getActivity).methodSentData(String exampledata) and ((MainActivity).getActivity).methodGetData(); to get data in the fragment.
methodSentData implements intent.putExtra("data",data); to sent data to other activity and methodGetData() implements getintent().GetStringExtras("data");
You can use local broadcast receiver.

Is possible to use finish() in to return in previous activities like C>B>A?

Technically I have a MainActivity "A" then then I'll go to next activity which is activity "B" then there is the other activity which is Activity "C"
But when I use finish, the activity B and C returns to each other instead backing to the main activity
When you route from Activity A to B then c, and you pressing back from C to B and A. You don't need to use finish() anywhere. Because android maintain stack for back button. It automatically push all activities and pop activities when back.
Refer this : https://developer.android.com/guide/components/tasks-and-back-stack.html
The loading of JSON data part should be done in onCreate() method,because you dont want to re-load the data.override the onbackpressed() method of activity on the some action (like button click) of the A and B activity Or use finish.

How to send data between fragments with SlidingTabLayout

Basically I am trying to create an app that passes data filled in an EditText on on one fragment, into a TextView on another fragment on a button click(The buttons is in the first fragment with the EditText). I use the SlidingTabLayout. I have 2 java classes that both extend Fragment and both inflate separate xml layouts(in the onCreateView). I have a java MainActivty with a public class"SectionsPagerAdapter that extends FragmentPagerAdapter, which depending on the swipe of the user displays 1 of the 2 Fragment classes. I am really confused on how I can send data between the 2 fragment sot that from the EditText in 1 fragment can get sent to the TextView in the other fragment on a button click. Could the suggested solutions be explained as simple as possible because I am relatively new to Android Studio. Many thanks in advance.
As per my understanding, basically you want to pass data between two fragments.
You can use activity for that from where fragments are initialized.
you can do this.
in MainActivity.java:
have a function setData(Object obj) and getData() which returns that object.
From fragment:
You can call those function of activity to save your data and get your data.
Here's way:
MainActivity activity = (MainActivity) getActivity();
Object obj = activity.getData();
activity.setData(obj);
I hope it helps.

How do I call functions in fragment from another?

I put two fragments in an activity. What I want to do is to hide a view from say fragment A when I click a button in fragment B. I have the hiding function in fragment A but how do I call it in fragment B?
I tried:
((FragmentA)getActivity().getFragmentManager().findFragmentById(R.id.fragment_a)).hideLivePreview();
but it gives me a null pointer exception...please help
You do not want to do this in general. Fragment A and Fragment B should not directly communicate, since Fragment A and Fragment B may not both be on the screen at the same time all of the time. They might be side-by-side on a large screen but displayed via separate activities on smaller screens. IMHO, activities should mediate all communications between fragments. If you have two fragments that are too tightly coupled for that, they should not be separate fragments in the first place.
All that being said, you are getting a NullPointerException because there is no fragment with that ID in the activity.

Categories