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.
Related
I have made the one Main_Activity And made many Fragment.
For example I am open the First Fragment and Second Fragment open from the first.My question is if i pressed the back Button on device the application terminate.Can possible i pressed the back Button the Screen Back second Fragment to First again pressed the screen move on Main_Activity then app terminate?
You cannot directly open fragments. They need to be inside some activity.
If you want to control the action of pressing the back button, you need to override the onBackPressed() method of the activity.
Use getFragmentManager().popBackStack() to go back to previous fragment and override onBackPress() method if required.
I have used the concept of Fragments in my Android App. I want to close my App from a fragment Activity. It's easy to do it in normal Activities by using onBackPressed() function, but how to do it in Fragments?
Fragment never runs independent. Activity contain the fragment so if you want to go back just press back button.
If you have any button in fragment to do so then write getActivity().finish();inside button click method.
EDIT 1 if you want to push your app in background then write following code
getActivity().moveTaskToBack(true);
The user can quit your app by pressing the Home button or navigate using the Back Button. Having a button in your app which closes it goes against best practices suggested by Google.
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();
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
I've implemented a custom logo for my ActionBar for Android.
Here's the image.
The issue now that I'm facing is how do I allow myself to be able to click on that back arrow,and bring myself to the previous activity?A huge thank you :D!
Do the following two things in your code:
FIRST:
Go to the Activity's code in which you want to put the back arrow and write the following code after setContentView...
getActionBar().setDisplayHomeAsUpEnabled(true);
SECOND:
Go to AndroidManifest.xml file and under the activity tag of another activity(from which you want to move after clicking on back arrow) put the following code
<activity
android:name="---------------------"//No change here
android:label="--------------------"//No change here
android:parentActivityName="<YourPackageName>.PreviousActivityName >//**Put this code here**
Where PreviousActivity is the one to which you want to move after clicking the back arrow button.
FOR EXAMPLE:
Your code has 2 activities namely Act1 and Act2,and suppose you want to navigate from Act2 to Act1 by pressing the ActionBar back arrow button present in Act2...Here your parentActivity is Act1.
Put the following code in Act2.java:---
getActionBar().setDisplayHomeAsUpEnabled(true);
Now go to AndroidManifest.xml and put the following code under Act2 tag:
Let's say your package name is:com.example.stack
<activity
android:name="---------------------"//No change here
android:label="--------------------"//No change here
android:parentActivityName="com.example.stack.Act1 >//**Put this code here**
Here: http://developer.android.com/training/implementing-navigation/ancestral.html
you will find all answers for this question. If you don't understand something, feel free to ask ;)