Back Navigation in Android - android

I am beginner in android and trying to develop an android app in which I stuck in back navigation. My issue is :
How to manage backstack with activities and fragments.
A1 activity with frag1 calls A2 activity and A2 activity displays a user list where on click of a list to check user profile, call to A1 Activity with Frag 2.
On opening of Frag2 of A1 activity, We are using intent flag: flag_activity_reorder_to_front and adding frag1 to backstack with FragmentManager transaction
Now IF I click on back then It shows A1 Activity with frag1 instead of A2 Activity.
IF I don't add frag1 into backstack then on back, It works with first back but on second back it exits from the app.
Any suggestions?

Try using single activity and handle all fragment transactions in it using an interface. Activity should implement this interface.
Example:
public interface FragChanger {
int NEXT_FRAGHELLO =1;
int NEXT_FRAGSET = 2;
int NEXT_FRAGSELECT =3;
int NEXT_FRAGLOG=4;
int NEXT_FRAGCHAT=5;
void onFragmentChange(int nextFrag);
}
The following should be in Your activity:
#Override
public void onFragmentChange(int nextFrag) {
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
switch (nextFrag){
case NEXT_FRAGHELLO:
break;
case NEXT_FRAGSET:
FragSet fragSet = new FragSet();
ft.replace(containerId,fragSet,"fragset");
ft.addToBackStack(null);
ft.commit();
break;
case NEXT_FRAGSELECT:
FragSelect fragSelect = new FragSelect();
ft.replace(containerId,fragSelect,"fragselect");
ft.addToBackStack(null);
ft.commit();
break;
case NEXT_FRAGCHAT:
FragChat fragChat = new FragChat();
ft.replace(containerId,fragChat,"fragchat");
ft.addToBackStack(null);
ft.commit();}
break;
case NEXT_FRAGLOG:
ft.replace(containerId,fragLog,"fraglog");
ft.addToBackStack(null);
ft.commit();
break;
}
Handling back in your activity:
#Override
public void onBackPressed() {
Log.d(TAG,"button back pressed");
//Check which fragment is displayed an do whatever you need
//for example like this
if (getSupportFragmentManager().findFragmentById(containerId) instanceof FragLog){
Fragment fl = getSupportFragmentManager().findFragmentByTag("fraglog");
if (fl !=null){
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.remove(fl);
ft.commit();
return;
}
}
}
Of course, this is only an example, but may be useful in Your case

Related

not able to save 2nd fragment state as it goes to new activity

i have 2 fragments (1,2).
after a button click on (fragment 1) transition happens to (fragment 2) .now from (fragment 2) if a button is clicked i go to the (new_activity ).my problem is when i return from (new_activity) i want to go back to (fragment 2) & then to (fragment 1) but it directly goes to fragment 1 .i am not able to add (fragment 2) to the addTOBackStack.
// method to handle click event of category
public void onClick(View v) {
FragmentManager fragmentManager = getActivity()
.getSupportFragmentManager();
FragmentTransaction ft = fragmentManager.beginTransaction();
Bundle bundle = new Bundle();
int categoryId;
CategoryFragment a = new CategoryFragment();
Log.d("HomeFragment", v.getId() + "");
switch (v.getId()) {
case R.id.image_american_chiffon:
categoryId = 1;
bundle.putInt("categoryId", categoryId);
a.setArguments(bundle);
ft.replace(R.id.contentFrame, a);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.addToBackStack(null);
break;
case R.id.image_nagma:
categoryId = 2;
bundle.putInt("categoryId", categoryId);
a.setArguments(bundle);
ft.replace(R.id.contentFrame, a);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.addToBackStack(null);
break;
}
ft.commit();
Log.e("BACK STACK COUNT",fragmentManager.getBackStackEntryCount()+"");
}
Everytime you call replace method,new object of that fragment is being created.You can add all needed fragments at the beginning in the hidden state and then just show and hide according to your need.so you don't need to initialize everytime,just have to display the required fragment and hide the other fragment.
To Add fragment at the begining,do like this:
Fragment myFragment = new CategoryFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.add(R.id.fragmentContainer, myFragment, SOME_TAG);
transaction.hide(myFragment);
transaction.commit();
and Instead of calling replace() method,just hide and show the desired fragment
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.show(getFragmentManager().findFragmentByTag(fragmentTagToBeShown));
transaction.hide(getFragmentManager().findFragmentByTag(currentFragmentTag));
transaction.commit();

setCustomAnimations not working on back button press

I've got this issue where transitions from fragment A to B working fine but on backPress they do not. Looking around I saw many with this issue but none of the answers seems to help me out. I must be doing something wrong but have no idea what and I would love some help, it drives me nuts!
This is my logic:
Fragment A calling fragment B:
private void loadNextFragment(WeatherComplete[] weatherDataArrayList) {
FragmentManager fm = getFragmentManager();
MainFragment mf = MainFragment.newInstance();
mf.setVars(choosenCity, weatherDataArrayList);
FragmentTransaction ft = fm.beginTransaction();
ft.setCustomAnimations(R.anim.slide_in, R.anim.slide_iout, R.anim.slide_in, R.anim.slide_iout);
ft.addToBackStack(null);
ft.replace(R.id.frame, mf);
ft.commit();
}
Fragment B calling fragment C (Settings) from menu:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
final FragmentManager fm = getActivity().getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.setCustomAnimations(R.anim.slide_in, R.anim.slide_iout, R.anim.slide_in, R.anim.slide_iout);
ft.replace(R.id.frame, new Settings(), "settings");
ft.commit();
break;
I'm passing an object array from fragment A to fragment B, thus when clicking the back button on fragment C it will go back to fragment B but there is no object array to work with so in that case I want fragment C to go back to fragment A instead - I'm popping the backStack:
if (weatherDataObj == null) {
Log.d(TAG, "WEATHER DATA IS NULL");
FragmentTransaction ft = getFragmentManager().beginTransaction();
getFragmentManager().popBackStack();
// ft.remove(this);
ft.commit();
}
Try using this:
ft.setCustomAnimations(R.anim.slide_in, R.anim.slide_iout,
R.anim.slide_in, R.anim.slide_iout);

android fragment handle back button press not working

I am working fragment, I replaced FragmentA by fragment B.it's working perfect,but when I press back button I can't back Fragment A.
I tried to override onKeyDown method,but I can't override this method in fragment
this is a my source
CategoryViewPager fragment1 = new CategoryViewPager();
FragmentTransaction ft = getSupportFragmentManager()
.beginTransaction().addToBackStack("method");
ft.setCustomAnimations(R.anim.trans_left_in, R.anim.trans_left_out);
ft.replace(R.id.content_frame, fragment1, "fragment2");
ft.commit();
when I use CategoryViewPager and click back button I can't go back(fragment wich i replaced this fragment)
how can I solve my problem?
Call addToBackStack() before you commit the transaction:
getSupportFragmentManager().beginTransaction()
// Add this transaction to the back stack
.addToBackStack()
.commit();
No need to use tag in your case. just use like this
/*
* Add this transaction to the back stack.
* This means that the transaction will be remembered after it is
* committed, and will reverse its operation when later popped off
* the stack.
*/
fragmentTransaction.addToBackStack(null);
It is better to handle back button press through your activity unless your actvity host large number of fragments.When you press back button inside your fragment onBackPressed() method of your activity will be called which can be overridden and handled..So handling back button for fragments can be done in this way..
MainActvity
public static boolean isMainActivityShown ;
public static boolean isFragment1Shown=false ;
public static boolean isFragment2Shown=false ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
isMainActivityShown=true //inside onCreate method put isMainActivityShown true
.
.
.
}
.
.
Fragment currentFragment = new Fragment1();
isMainActivityShown=false; //when moving to fragment1
isFragment1Shown=true;
frgManager = getFragmentManager();
frgManager.beginTransaction().replace(R.id.content_frame, currentFragment)
.commit();
#Override
public void onBackPressed() {
if(isMainActivityShown)
{
finish();
}
else if(isFragment1Shown)
{
//write the code to handle back button when you are in Fragment1
}
else if(isFragment2Shown)
{ //When you are in Fragment 2 pressing back button will move to fragment1
Fragment currentFragment = new Fragment1();
isFragment2Shown=false;
isFragment1Shown=true;
FragmentManager frgManager;
frgManager = getFragmentManager();
frgManager.beginTransaction().replace(R.id.content_frame, currentFragment)
.commit();
}
}
Fragment1
Fragment currentFragment = new Fragment2();
MainActivity.isFragment1Shown=false;
MainActivity.isFragment2Shown=true;
frgManager = getFragmentManager();
frgManager.beginTransaction().replace(R.id.content_frame, currentFragment)
.commit();
Check this link
Check the addToBackStack(String name) method. This should solve your problem.

Back button closing app even when using FragmentTransaction.addToBackStack()

None of the other questions I have read on stackoverflow have been able to help with my problem. As far as I can tell, I am doing everything correctly.
I have a master/detail flow with fragments.
Upon creation of the main activity, the master fragment is loaded with the following code:
Fragment frag;
frag = new MainListFragment();//<-- **the master fragment**
FragmentManager fm = getFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
transaction.replace(R.id.fragment_container, frag);
Log.d("My Debug Bitches", "stack:" + fm.getBackStackEntryCount());
transaction.commit();
The master fragment has a ListView; clicking on a list item brings up the details fragment like so:
#Override
public void onListItemClick(ListView listView, View view, int position, long id) {
super.onListItemClick(listView, view, position, id);
FragmentManager fm = getFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
SubListFragment frag = new SubListFragment();//<-- **the detail fragment**
transaction.replace(R.id.fragment_container, frag);
transaction.addToBackStack(null);
transaction.commit();
fm.executePendingTransactions();
Log.d("My Debug Bitches", "stack:" + fm.getBackStackEntryCount());
}
Now, according to LogCat, the BackStackEntryCount changes from 0 to 1 after I navigate from master fragment to detail fragment:
So why is it that, when I click the back button while in the details fragment, that the app closes instead of returning to the master fragment??????????
You have to add the popBackStack() call to the onBackPressed() method of the activity.
Ex:
#Override
public void onBackPressed() {
if (fragmentManager.getBackStackEntryCount() > 0) {
fragmentManager.popBackStack();
} else {
super.onBackPressed();
}
}
#Bobbake4's answer is awesome, but there is one little problem.
Let's say I have three fragments A, B and C.
A is the main Fragment (the fragment that shows when I launch my app), B and C are fragments I can navigate to from the navigation drawer or from A.
Now, when I use the back button from B or C, I go back to the previous fragment (A) alright, but the title of the previous fragment (fragment B or C) now shows in the actionBar title of Fragment A. I have to press the back button again to "truly" complete the back navigation (to display the view and correct title for the fragment and returning to)
This is how I solved this problem. Declare these variables.
public static boolean IS_FRAG_A_SHOWN = false;
public static boolean IS_FRAG_B_SHOWN = false;
public static boolean IS_FRAG_C_SHOWN = false;
In the MainActivity of my app where am handling navigation drawer methods, I have a method displayView(position) which handles switching of my fragments.
private void displayView(int position) {
IS_FRAG_A_SHOWN = false;
IS_FRAG_B_SHOWN = false;
IS_FRAG_C_SHOWN = false;
// update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 0:
fragment = new FragmentA();
IS_FRAG_A_SHOWN = true;
break;
case 1:
fragment = new FragmentB();
IS_FRAG_B_SHOWN = true;
break;
case 2:
fragment = new FragmentC();
IS_FRAG_C_SHOWN = true;
break;
default:
break;
}
finally, in my onBackPressed method, I do this:
public void onBackPressed() {
if(fragmentManager.getBackStackEntryCount() != 0) {
fragmentManager.popBackStack();
if (IS_FRAG_A_SHOWN) { //If we are in fragment A when we press the back button, finish is called to exit
finish();
} else {
displayView(0); //else, switch to fragment A
}
} else {
super.onBackPressed();
}
}

How to resume existing Fragment from BackStack

I am learning how to use fragments. I have three instances of Fragment that are initialized at the top of the class. I am adding the fragment to an activity like this:
Declaring and initializing:
Fragment A = new AFragment();
Fragment B = new BFragment();
Fragment C = new CFragment();
Replacing/Adding:
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, A);
ft.addToBackStack(null);
ft.commit();
These snippets are working properly. Every fragment is attached to the activity, and is saved to the back stack without any problem.
So when I launch A, C, and then B, the stack looks like this:
| |
|B|
|C|
|A|
___
And when I press the 'back' button, B is destroyed and C is resumed.
But, when I launch fragment A a second time, instead of resuming from back stack, it is added at the top of the back stack
| |
|A|
|C|
|A|
___
But I want to resume A and destroy all fragments on top of it (if any). Actually, I just like the default back stack behavior.
How do I accomplish this?
Expected: (A should be resumed and top fragments should be destroyed)
| |
| |
| |
|A|
___
Edit: (suggested by A--C)
This is my trying code:
private void selectItem(int position) {
Fragment problemSearch = null, problemStatistics = null;
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction ft = manager.beginTransaction();
String backStateName = null;
Fragment fragmentName = null;
boolean fragmentPopped = false;
switch (position) {
case 0:
fragmentName = profile;
break;
case 1:
fragmentName = submissionStatistics;
break;
case 2:
fragmentName = solvedProblemLevel;
break;
case 3:
fragmentName = latestSubmissions;
break;
case 4:
fragmentName = CPExercise;
break;
case 5:
Bundle bundle = new Bundle();
bundle.putInt("problem_no", problemNo);
problemSearch = new ProblemWebView();
problemSearch.setArguments(bundle);
fragmentName = problemSearch;
break;
case 6:
fragmentName = rankList;
break;
case 7:
fragmentName = liveSubmissions;
break;
case 8:
Bundle bundles = new Bundle();
bundles.putInt("problem_no", problemNo);
problemStatistics = new ProblemStatistics();
problemStatistics.setArguments(bundles);
fragmentName = problemStatistics;
default:
break;
}
backStateName = fragmentName.getClass().getName();
fragmentPopped = manager.popBackStackImmediate(backStateName, 0);
if (!fragmentPopped) {
ft.replace(R.id.content_frame, fragmentName);
}
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.addToBackStack(backStateName);
ft.commit();
// I am using drawer layout
mDrawerList.setItemChecked(position, true);
setTitle(title[position]);
mDrawerLayout.closeDrawer(mDrawerList);
}
The problem is, when I launch A and then B, then press 'back', B is removed and A is resumed. and pressing 'back' a second time should exit the app. But it is showing a blank window and I have to press back a third time to close it.
Also, when I launch A, then B, then C, then B again...
Expected:
| |
| |
|B|
|A|
___
Actual:
| |
|B|
|B|
|A|
___
Should I override onBackPressed() with any customization or am I missing something?
Reading the documentation, there is a way to pop the back stack based on either the transaction name or the id provided by commit. Using the name may be easier since it shouldn't require keeping track of a number that may change and reinforces the "unique back stack entry" logic.
Since you want only one back stack entry per Fragment, make the back state name the Fragment's class name (via getClass().getName()). Then when replacing a Fragment, use the popBackStackImmediate() method. If it returns true, it means there is an instance of the Fragment in the back stack. If not, actually execute the Fragment replacement logic.
private void replaceFragment (Fragment fragment){
String backStateName = fragment.getClass().getName();
FragmentManager manager = getSupportFragmentManager();
boolean fragmentPopped = manager.popBackStackImmediate (backStateName, 0);
if (!fragmentPopped){ //fragment not in back stack, create it.
FragmentTransaction ft = manager.beginTransaction();
ft.replace(R.id.content_frame, fragment);
ft.addToBackStack(backStateName);
ft.commit();
}
}
EDIT
The problem is - when i launch A and then B, then press back button, B
is removed and A is resumed. and pressing again back button should
exit the app. But it is showing a blank window and need another press
to close it.
This is because the FragmentTransaction is being added to the back stack to ensure that we can pop the fragments on top later. A quick fix for this is overriding onBackPressed() and finishing the Activity if the back stack contains only 1 Fragment
#Override
public void onBackPressed(){
if (getSupportFragmentManager().getBackStackEntryCount() == 1){
finish();
}
else {
super.onBackPressed();
}
}
Regarding the duplicate back stack entries, your conditional statement that replaces the fragment if it hasn't been popped is clearly different than what my original code snippet's. What you are doing is adding to the back stack regardless of whether or not the back stack was popped.
Something like this should be closer to what you want:
private void replaceFragment (Fragment fragment){
String backStateName = fragment.getClass().getName();
String fragmentTag = backStateName;
FragmentManager manager = getSupportFragmentManager();
boolean fragmentPopped = manager.popBackStackImmediate (backStateName, 0);
if (!fragmentPopped && manager.findFragmentByTag(fragmentTag) == null){ //fragment not in back stack, create it.
FragmentTransaction ft = manager.beginTransaction();
ft.replace(R.id.content_frame, fragment, fragmentTag);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.addToBackStack(backStateName);
ft.commit();
}
}
The conditional was changed a bit since selecting the same fragment while it was visible also caused duplicate entries.
Implementation:
I highly suggest not taking the the updated replaceFragment() method apart like you did in your code. All the logic is contained in this method and moving parts around may cause problems.
This means you should copy the updated replaceFragment() method into your class then change
backStateName = fragmentName.getClass().getName();
fragmentPopped = manager.popBackStackImmediate(backStateName, 0);
if (!fragmentPopped) {
ft.replace(R.id.content_frame, fragmentName);
}
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.addToBackStack(backStateName);
ft.commit();
so it is simply
replaceFragment (fragmentName);
EDIT #2
To update the drawer when the back stack changes, make a method that accepts in a Fragment and compares the class names. If anything matches, change the title and selection. Also add an OnBackStackChangedListener and have it call your update method if there is a valid Fragment.
For example, in the Activity's onCreate(), add
getSupportFragmentManager().addOnBackStackChangedListener(new OnBackStackChangedListener() {
#Override
public void onBackStackChanged() {
Fragment f = getSupportFragmentManager().findFragmentById(R.id.content_frame);
if (f != null){
updateTitleAndDrawer (f);
}
}
});
And the other method:
private void updateTitleAndDrawer (Fragment fragment){
String fragClassName = fragment.getClass().getName();
if (fragClassName.equals(A.class.getName())){
setTitle ("A");
//set selected item position, etc
}
else if (fragClassName.equals(B.class.getName())){
setTitle ("B");
//set selected item position, etc
}
else if (fragClassName.equals(C.class.getName())){
setTitle ("C");
//set selected item position, etc
}
}
Now, whenever the back stack changes, the title and checked position will reflect the visible Fragment.
I think this method my solve your problem:
public static void attachFragment ( int fragmentHolderLayoutId, Fragment fragment, Context context, String tag ) {
FragmentManager manager = ( (AppCompatActivity) context ).getSupportFragmentManager ();
FragmentTransaction ft = manager.beginTransaction ();
if (manager.findFragmentByTag ( tag ) == null) { // No fragment in backStack with same tag..
ft.add ( fragmentHolderLayoutId, fragment, tag );
ft.addToBackStack ( tag );
ft.commit ();
}
else {
ft.show ( manager.findFragmentByTag ( tag ) ).commit ();
}
}
which was originally posted in
This Question
Step 1: Implement an interface with your activity class
public class AuthenticatedMainActivity extends Activity implements FragmentManager.OnBackStackChangedListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
.............
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().add(R.id.frame_container,fragment, "First").addToBackStack(null).commit();
}
private void switchFragment(Fragment fragment){
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).addToBackStack("Tag").commit();
}
#Override
public void onBackStackChanged() {
FragmentManager fragmentManager = getFragmentManager();
System.out.println("#Class: SummaryUser : onBackStackChanged "
+ fragmentManager.getBackStackEntryCount());
int count = fragmentManager.getBackStackEntryCount();
// when a fragment come from another the status will be zero
if(count == 0){
System.out.println("again loading user data");
// reload the page if user saved the profile data
if(!objPublicDelegate.checkNetworkStatus()){
objPublicDelegate.showAlertDialog("Warning"
, "Please check your internet connection");
}else {
objLoadingDialog.show("Refreshing data...");
mNetworkMaster.runUserSummaryAsync();
}
// IMPORTANT: remove the current fragment from stack to avoid new instance
fragmentManager.removeOnBackStackChangedListener(this);
}// end if
}
}
Step 2: When you call the another fragment add this method:
String backStateName = this.getClass().getName();
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.addOnBackStackChangedListener(this);
Fragment fragmentGraph = new GraphFragment();
Bundle bundle = new Bundle();
bundle.putString("graphTag", view.getTag().toString());
fragmentGraph.setArguments(bundle);
fragmentManager.beginTransaction()
.replace(R.id.content_frame, fragmentGraph)
.addToBackStack(backStateName)
.commit();
I know this is quite late to answer this question but I resolved this problem by myself and thought worth sharing it with everyone.`
public void replaceFragment(BaseFragment fragment) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
final FragmentManager fManager = getSupportFragmentManager();
BaseFragment fragm = (BaseFragment) fManager.findFragmentByTag(fragment.getFragmentTag());
transaction.setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_left, R.anim.enter_from_left, R.anim.exit_to_right);
if (fragm == null) { //here fragment is not available in the stack
transaction.replace(R.id.container, fragment, fragment.getFragmentTag());
transaction.addToBackStack(fragment.getFragmentTag());
} else {
//fragment was found in the stack , now we can reuse the fragment
// please do not add in back stack else it will add transaction in back stack
transaction.replace(R.id.container, fragm, fragm.getFragmentTag());
}
transaction.commit();
}
And in the onBackPressed()
#Override
public void onBackPressed() {
if(getSupportFragmentManager().getBackStackEntryCount()>1){
super.onBackPressed();
}else{
finish();
}
}
getFragmentManager().addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
#Override
public void onBackStackChanged() {
if(getFragmentManager().getBackStackEntryCount()==0) {
onResume();
}
}
});
Easier solution will be changing this line
ft.replace(R.id.content_frame, A);
to ft.add(R.id.content_frame, A);
And inside your XML layout please use
android:background="#color/white"
android:clickable="true"
android:focusable="true"
Clickable means that it can be clicked by a pointer device or be tapped by a touch device.
Focusable means that it can gain the focus from an input device like a keyboard. Input devices like keyboards cannot decide which view to send its input events to based on the inputs itself, so they send them to the view that has focus.

Categories

Resources