Handle back pressed of multiple fragments in android - android

Hi Currently am developing application using fragments.Totally i have 15 fragments and loaded it in single Activity.While onBack Pressing on Each fragment will launch previous fragment.I google about it and also i got answers for it.but i just want to know which is more efficient way handle this.

By calling addToBackStack(), the replace transaction is saved to the back stack so the user can reverse the transaction and bring back the previous fragment by pressing the Back button. Reference
If you add multiple changes to the transaction (such as another add() or remove()) and call addToBackStack(), then all changes applied before you call commit() are added to the back stack as a single transaction and the Back button will reverse them all together.
Just add this transaction.addToBackStack(null);
For more information you may visit Handling back button press Inside Fragments

Related

Popping Fragment from backstack on BACK key press

I am adding a Fragment and transaction is committed to the back stack using the following code:
getSupportFragmentManager().beginTransaction().add(id, fragment, "TAG").addToBackStack(null).commit();
Now when is press the BACK key. Activity is getting finished. But I just want to remove the fragment.
Should I manually handle the BACK key press and do the task of popping backstack or is there any way I can make activity to handle this automatically.
I found one reason for why it may happen:
If you are using app.Activity but making transactions with SupportFragmentManager, or using support.Activity and making transactions with FragmentManager, it seems that the activity ignores the fragments back-stack.
The usage should be consistent, support or normal.

Prevent DialogFragment 'A' reappearing when selection made on DialogFragment 'B'

I have a DialogFragment, call it A, which presents an option that leads to a second DialogFragment, B, being displayed. B provides further options.
The functionality I require is as follows:
Making a selection in A leads to B being displayed (as stated above).
If the user hits back while B is being displayed, A should be resumed into view.
If the user makes a selection in B, then B should dismiss and A should not reappear.
In A, inside an onItemClick() handler I cause B to appear using:
FragmentManager manager = getActivity().getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.remove(DialogFragmentA.this);
transaction.addToBackStack("transaction_label");
DialogFragmentB dialogFragment = DialogFragmentB.newInstance( ...some args here...);
dialogFragment.show(transaction, "frag_B");
I call .addToBackStack() as I understand this will cause the back key to pop and reverse the transaction. That is, replace B with A again.
So far, requirements 1 and 2 are met.
B makes use of AlertDialog.Builder. A positive button is used with listener. When that positive button is pressed, I want requirement 3 to be met. That is, B should dismiss and A should not reappear. But what actually happens is A appears again.
I am assuming here that within the implementation of AlertDialog's positive button is a call to dismiss() which causes the back stack to be popped, resulting in A appearing again. Is this the case?
What I have tried to do is, within the positive button's onClick(), is to obtain the FragmentManager and call .popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE). But this has no apparent effect; A continues to appear. Using popBackStackImmediate() has no effect either.
Is this perhaps because the event loop has already somehow committed to popping the back stack by the time the positive button listener's onClick() executes?
I'd be grateful for an explanation for what is occurring and how I can make it work as intended.
I think I have nailed it, helped by the question and answer here.
The key point is that the sequence of transactions is nothing -> A -> B. I want to pop the back stack to the point where I have nothing. Therefore, as well as calling .addToBackStack() for the A -> B transaction, I also need to call .addToBackStack() for the nothing -> A transaction. Then, all of those transactions can be popped off the back stack again to get back to the point of nothing: The first pop does B -> A, then the next pop does A -> nothing.
For both transactions I am now calling .addToBackStack("some_transaction"). In B's positive onClick(), I call manager.popBackStack("some_transaction", FragmentManager.POP_BACK_STACK_INCLUSIVE);. I might rethink the tags I'm giving to the transactions, but this is now working as I want it to.
So the basic point seems to be that, if you want it to be possible to go back to a DialogFragment but also go back to the point where that DialogFragment wasn't shown at all, you have to add the transaction that put it there in the first place onto the back stack, so you can pop back to that point.
I previously thought it would be possible to achieve what I wanted by only adding the A -> B transaction to the back stack, and then somehow completely purging (rather than popping) the back stack to prevent A appearing again once a selection had been made in B. But it seems that there is no way to actually 'purge' the back stack; you can only pop and reverse the transactions.

Handling to reopen fragment on back press

Let me explain my application first.
I have an activity and fragments in it. I'm opening fragmentA onCreate of application. In fragmentA,before i open fragmentB by using support fragment manager's replace() method, i call a webService for some json datas.
Then i open fragmentB with these json datas. After i opened the fragmentB, i want to turn back to fragmentA by using device's default backbutton, and i don't want to handle backButtonPress(). Just go back to previous fragment. My app does this.
Here is the question. When i press back button, my fragmentA calls the service again and it affects the user to wait. But how can i just turn to the previous fragment ?
I am looking for the best solution to turn previous fragment.

Single activity Fragment based app and back button

I got the app, that uses practcally just one activity. There is main area where I put fragments into. But what about back button now?
Of course I can override onBackPressed() but with what?
I'm pushing a fragment into it's holder using FragmentTransaction.replace() method every time. I might be lacking understanding of a subject, but shouldn't there be some fragment built-in stack, that would allow me to point onBackPressed() to the previous fragment in stack?
Do not override onBackPress. You can add any fragment transaction to back stack. There is special method to do this.

back button doesn't work when I want to return to the previous fragment

I manage two fragments in my MainActivity.
One of them is a subclass of ListFragment to show a list of items.
The main idea is to navigate to another list view when user tap one of the items, and the user can go back to the previous list view when tapping back button.
The code for transmit to a new list is shown as follow:
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
NewFragment newFragment = new NewFragment();
newFragment.setArguments(getIntent().getExtras());
transaction.replace(R.id.fragment_layout, newFragment);
transaction.addToBackStack(null);
transaction.commit();
However, I just simply exit the application other than going back to the previous view.
What am I doing wrong?
unlike activities, with fragments you have to explicitly add things onto the "back stack". basically, when your app displays a new fragment such that'd you like back to return to the preview fragment, you call FragmentTrasaction.addToBackStack().
the framework handles popping the fragment off the back stack when the user presses back. if you need something more complex, you can override the back button press for your fragment. this question covers that,
Android Fragment handle back button press
The back button closes the top Activity and does not navigate back in your Fragment history. You have to do that yourself with popBackStack()

Categories

Resources