i have to jump from activity A to fragment2 in activity B(in the activityB, i use the viewpager,which contains four fragment).what i want is when i use the upNavigation of the actionbar ,i just jump to the exact second fragment.
can anyone help me?(i have searched for some related questions ,but it seems they all navigate in/between the fragments,which is not my want.)
Use this , detect the onclick
ViewPager.setCurrentItem(pageNumber);
above line will navigate you to that page
Related
I have a simple application consisting of one MainActivity and two fragments, FragmentA and FragmentB. I want to use a NavigationGraph such that users still land on MainActivity but then can use two buttons to navigate to either FragmentA or FragmentB. Is there a way to do that? Based on the stuff on DAC, it looks like I have to use three fragments plus the MainActivity. But I was wondering if I can just use my existing scheme of 1 Activity and 2 Fragments.
Adding details to avoid confusion?
MainActivity has two buttons: User clicks on "Do A" to go to FragmentA and on "Do B" to go to FragmentB.
However, to use the Navigation Graph, it looks like I have to add one more fragment -- MainFragment. My question is: is there a way to avoid having to add an additional fragment and still use Navigation Graph.
Android applications need at least one activity to show UI. A fragment is just like a button in an android app. You cant show it without attaching it to an activity. In the android navigation tutorial, there's one activity. That activity only contains tag so it is actually empty. In fragment tag, you show a fragment and that tutorial show you how to switch fragment inside tag. So you are always inside MainActivity even when your app show a different UI(switching between fragment).
just like ianhanniballake comment above.. What does your activity show? What do you expect it to show? you always see it.
Edit:
Navigation graph only works for fragments. If you want to navigate between activities, use this Code:
Intent switchActivityIntent = new Intent(this, TargetActivity.class);
startActivity(switchActivityIntent);
You can use navigation graph like usual and when you need to open activity just use that code. It works fine.. But why do u need to use more than one activity when u using navigation graph? you should only have one activity when using navigation graph. That is why google make that thing(navigation graph) to support Single-Activity architecture.
I am working on a project with only one activity, my activity has fragment container to swap between different fragments and thats it. I have like 15-20 fragments in the project and I want to be able have a back button between some of the fragments. As I do only have one activity and I have NoActionBar I was wondering if there is a way to have an up/back button between fragments. I have researched but not found an awnser.
Thanks in advance.
Try use the following inside the onclick() of the back button: getActivity().onBackPressed();
This question has also been answered here:Fragment pressing back button.
Check that link for more insights.
I'm trying to make an app which has the DrawerActivity as the main activity. I have implemented 8 fragments within it, which correspond to each item in the drawer. Now, the problem I have is, whenever I try to press the back button to go back to the DrawerActivity from the fragment, I end up exiting the app instead. I've been searching on forums for three days now, and have not found any solution to this. Quite frustrated, I stupidly deleted my code, so I can't really show it right now. Can anyone simply explain to me how I should exit a fragment using the back button, and return to my main activity?
Current scenario:
DrawerActivity contains eight fragments, all independent of each other. Let's call them fragments A through H. When I go to fragment A, I should be able to open the navigationDrawer and head to any other fragments from B to H. However, on pressing the back button, I should also be able to go back to the drawer activity's main page.
Things I have tried which didn't work are
1. Using onBackPressed and popBackStack.
2. Creating custom listener.
3. Using fragmentTransaction.
I can't believe how stupid I am. I was using my Gmail app on my phone when I suddenly realized that the first fragment in the drawer activity is always the default fragment, and the app should always revert to it on the back button press. What I was thinking was that the drawer activity would go back to the original state when back is pressed, but I didn't account for the fact that fragments are part of the activity itself.
Thank you all for trying to understand my problem, which wasn't really a problem at all... It seems I learned something in spite of myself.
Have you tried overriding onBackPressed() without calling the superclass method? (So that you prevent finish() from being called.)
#Override
public void onBackPressed() {
//show Fragment A
}
Call it without the "super.onBackPressed()" and it should work.
You must add your fragment to the android back Stack like this:
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.your_container,fragment,fragment.TAG)
.addToBackStack(null)
.commit();
On my app I am using a Main Activity, which has a Navigation Drawer, and as the user go to an options from the drawer, it will change the fragment beging displayed accordingly to the option selected.
If the user hit's "Back button" several times, it will go back to a point in which it will reach my Main Activity, which is a blank and empty layout.
When I reach this point (my main activit, empty), I would like to exit the app, or, open the Navigation Drawer.
The problem is, I don't know any event that shows me that I am back to the Main Activity. I checked the onResume, but it's never called, which makes sense, since the main activity has never been stopped.
I thought perhaps there would be an event from the fragment manager that would be called on the Main Activity when a fragment was detached, and from there I could check if there was no fragment at all attached?
When you push your first fragment, add a tag to it. Something like
transaction.replace( R.id.rootContainer, firstFragment, "rootFragment");
Whenever user presses back button, you can get your rootFragment from FragmentManager
FirstFragment myFragment = (FirstFragment) getSupportFragmentManager().findFragmentByTag("rootFragment");
And check if this fragment is visible or not, by myFragment.isVisible(), if not, then keep popping the stack, if it is visible, it means user is on the first fragment. Now you can either quit the app, or show your menu drawer.
Good night good sir. Thank you for your tip.
I used a different approach, based on your repply, that gave me quite a few insights.
On main activity, I Overrided the onKeyDown and check if it was pressed the Back Button. If yes, then I check the size of my Back Stack.
Uppon that, I decide if I want to finish my application.
Also, thanks for the tip of "labeling" the fragments on the back stack, didn't know I could do that.
I have navigation drawer which using mostly fragments for loading views but one activity which confuse me.you can see my drawer below and assume that i have to start activity when clicking the option Profile.if i starting the activity do i need to return to home screen or i need to return to the fragment from where the activity launched ?
Problem 2 : Am replacing the selected drawer icon as you can see in the attachment below.when clicking the Profile option do i need to replace the Profile option icon as selected which is not feeling comfortable for me.?
And the genuine Problem : Drawer look like stucked or not properly ie , not smoothly closing when calling startActivity.
And one more : What about finishing navigation activity when launching new activity and starting freshly after finish the new one ?
Answer for problem 1: It is depend on you where you are writing a code i. e. click event for drawer layout. if you are using fragment then write a code in fragment and fragment is a part of activity so obviously you will going to use getActivity().startActivity(intent);
Answer for problem 2: This is not mandatory that use have to replace a icon. you can use tint functionality to change the icon color or you can use selector or use can use ripple which is default in android 5.0 and above.
Answer for problem 3: Sorry i am facing also some problem so will update on this soon.
Answer for problem 4: Why you are finishing navigation activity. Ultimately you will come back to navigation activity so don't finish navigation activity. Just start new activity. once new activity finish it will come back to navigation activity.
Hope this will help for you.