I've implemented many fragments using the navigation graph. Consider I have A-> B-> C-> D
where A, B, C, D are the fragments in order of implementation. Now I am on fragment D and I want to move to fragment C on click of a button in D. How do I do that? Also when I reach fragment C and press the back button it should take me to fragment B. I want to implement navigation to the previous fragment and destroy the current fragment. i.e Move to C from D and destroy D(not want it on the press of a back button).
u can use below code you can get current position of your viewpager :
int position = mViewPager.getCurrentItem();
//or
Fragment fragment = adapter.getItem(position);
so also can use this code to dynamicity go which tab u want, so in button click listener set this code:
viewpager.setCurrentItem(specificFragmentPosition);
// like this: viewpager.setCurrentItem(2);
and for second question u have to override onBackPressed() method in your fragment
this method calls when user clicked in back button...
so u can add this code again in your onBackPressed() method,
viewpager.setCurrentItem(specificFragmentPosition);
Related
This one is weird. I'm implementing breadcrumb navigation on a older app and I'm not at the point where it's ready to refactor the views. Unfortunately every new navigation opens a new activity and sometimes the new instances of the same activity. So my breadcrumb navigations basically look like this:
Activity A > Activity B > Activity C > Activity C > Activity C > Activity C > Activity D
So if I'm on Activity C and I need to go back to Activity B I can just set the Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP for the intent and navigate back no problem. But if I need to navigation from the 4 instance of Activity C to the 2nd instance of Activity C I'm not able to use any intent flags to make that happen. My app is already making use of the back button so I cannot override that to utilize here. Does anyone have any ideas as to navigation among the multiple instance of Activity C?
Fortunately all these app do inherit from the same parent app which extends FragmentActivity if that is helpful.
To go back one item in your breadcrumb you don't have to use the BACK button, you can just call finish(), which will finish the current Activity and go back to the previous one in the stack.
To go back to a specific Activity, you can use startActivity() with flags Intent.FLAG_ACTIVITY_CLEAR_TOP and Intent.FLAG_ACTIVITY_SINGLE_TOP, as you have already indicated in your question.
Anything else is not possible. See my answer to a related question here:
https://stackoverflow.com/a/39104837/769265
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
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.
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.
I am adding a fragment in an activity, fragment A.
Now from this fragment, I go to various other fragments, and whenever I go to those, I replace fragment A.
But there is only one scenario when i want that when i am about to go from fragment A to fragment Z, I add fragment A to the back stack.
So basically for all the clicks on fragment A, which take the user to other fragments (by replacing fragment A), there is only click where I want that fragment A be put in the back stack, this i want as when i press back from this new fragment Z, I do not want the activity to finish, rather i want fragment A to just come back up.
Now one dirty way of doing this is to do the following:
#Override
public void onBackPressed() {
if ( getFragmentManager().findFragmentByTag(FragmentZ.class.toString()) == null){
super.onBackPressed();
}
else {
//i would add fragment A over here.
}
}
But I was thinking that if i could add fragment A into the back stack just before i go to fragment Z then i do not have to take care of the above.
Could some please help me with the above?
The only way you can add Fragment A to the back stack is before you commit the transaction which replaces Fragment A with Fragment Z.
You can not add it later on after Fragment Z is already added.
Before calling commit() on the transaction which inserts Fragment Z, call addToBackStack(null) on the the transaction.
Then you will not have to override onBackPressed().