Fragments and onBackPressed function - android

I am using a navigation drawer and fragments to inflate the main activity.
However when I press the back button it closes the application which is obvious with fragment setup.
What I want is that when the user clicks the back button they go to the home page fragment if its not currently the view, but if they are on the home page I want the app to close.
How to go around this problem?

Overriding the back button would be a REALLY BAD idea, as it will hurt more than help.
If you want to add a Fragment that will not be replaced (Your initial "HomePageFragment"), just add this line:
transaction.addToBackStack(null);
Which will give you something like:
final FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment, fragment, TAG_FRAGMENT);
transaction.addToBackStack(null);
transaction.commit();
If you add some other Fragments, don't add them to the back stack.

You should override the onBackPressed method in the activity class and using that you can determine whether you need to switch back to your home fragment or call super.onBackPressed().

Related

how to maintain the fragment depend upon back button?

I've got a navigation drawer. In that navigation drawer I've got five fragments. When I select a fragment, it shows up. When I press the back button, it goes to the previous fragment. However, I need the back button to send you back to the first fragment. How do I do this?
Overwrite the onBackPressed() method in your Activity:
#Override
public void onBackPressed(){
FragmentTransactionn ft = getFragmentManager().beginTransaction();
ft.replace([your fragment container], yourfirstFragment, TAG_FRAGMENTFIRST);
ft.commit();
}
Apparently you already figured out how to show fragments. I'd suggest using the same code you use in your navigation drawer in the public void onBackPressed() to draw up the first fragment again. To make users able to exit the app, check if the first fragment is already visible. If so, call super.onBackPressed() or finish().

Leaking Fragments from FragmentManager

I'm struggling with a Fragment leak and I am not sure how to handle it:
I have a FragmentActivity(I'm using support lib v4) with two buttons: forth and back. Forth button adds a fragment to the backstack with a long animation and back button pops the fragment from the backstack.
Code for forth button click listener:
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.setCustomAnimations(R.anim.own_slide_in_left, R.anim.own_slide_out_right, R.anim.own_slide_in_left, R.anim.own_slide_out_right);
transaction.add(R.id.fragment_holder, new FirstFragment());
transaction.addToBackStack(null);
transaction.commit();
And for back button click listener:
getSupportFragmentManager().popBackStackImmediate();
When I press the back button and rotate the device while the animation is ongoing then after rotation visually everything seems to be ok, but the previous Fragment is leaked. I can tell it by having a breakpoint in, for example, Fragment's ctor or onSaveInstanceState(). It will be it on each rotation when it shouldn't be.
What am I doing wrong?
EDIT: This problem persists using native Fragments, Android 4.0.3

How to get a reference to the last backstack popped fragment?

I add, show and hide fragments. Each time I add/show a fragment, I hide the previous fragment and add the transaction to the backstack.
When a user presses the back button, a fragment is popped and I would like to have a reference to it.
Why do I need a reference? So I could hide it when the user continues to the next fragment.
So, how do I get a reference to a popped fragment?
EDIT-25-04-2013:
Here's code to explain how to add a new fragment, while hiding the previous one. The question is how to get a reference to the last fragment after it is popped from the backstack (using the back button)?
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.hide(lastFragment);
fragmentTransaction.add(newFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
lastFragment = newFragment;
I use generated tags for each fragment, save the tags in a stack and persist the stack. This way I get hold of every fragment out there - last one in specific.
See the code here.

Not return to dialogfragment after fragment

I have Fragment1. On them, i pressed button and start DialogFramgent. On DialogFragment i pressed button and start Fragment2. When i pressed back, i am return to Fragment1, but not to DialogFragmnet
You can add the fragment to the backstack. A call addToBackStack() should solve the problem. Straight from the documentation: "This back stack is managed by the activity and allows the user to return to the previous fragment state, by pressing the Back button". See for details:
http://developer.android.com/guide/components/fragments.html
Jasper
A great feature about using fragments in your activity is the ability to add, remove, replace, and perform other actions with them, in response to user interaction. Each set of changes that you commit to the activity is called a transaction and you can perform one using APIs in FragmentTransaction. You can also save each transaction to a back stack managed by the activity, allowing the user to navigate backward through the fragment changes (similar to navigating backward through activities).
You can acquire an instance of FragmentTransaction from the FragmentManager like this:
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Each transaction is a set of changes that you want to perform at the same time. You can set up all the changes you want to perform for a given transaction using methods such as add(), remove(), and replace(). Then, to apply the transaction to the activity, you must call commit().
Before you call commit(), however, you might want to call addToBackStack(), in order to add the transaction to a back stack of fragment transactions. This back stack is managed by the activity and allows the user to return to the previous fragment state, by pressing the Back button.

Why is Fragment.addToBackStack() causing the Back button to do nothing?

Activity 1 is visible. Press a button, and Activity 2 opens.
Activity 2 adds fragment A to itself (and back stack) and it displays fine
Pressing a button within the fragment transitions to another fragment, B
Press Back. Nothing happens. Huh? The Back press is seemingly absorbed and not acted upon, the display remains the same.
Press Back a second time, it reverts to the Activity 1, as expected.
Why is my fragment not being shown in step 4? I've added the fragment to the back stack, so why (when the Back button seems aware of its existence) does it not show the fragment?
Here's the code I'm using in Activity 2 to open Fragment A.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act_profile_edit);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
transaction.addToBackStack(null);
transaction.add(android.R.id.content, new MyFragment());
transaction.commit();
}
And here's the code to open Fragment B
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
transaction.add(android.R.id.content, new MyOtherFragment());
transaction.commit();
Have you tried transaction.replace(...) instead of transaction.add(...)? That should work. I'm guessing because if you're just adding a fragment over another, it doesn't see transaction as wanting to go back fro Fragment A.
EDIT
The actual answer for the question is below in the comments: addToBackStack() should be used on the fragment which is replacing, not the one being replaced.

Categories

Resources