I have 3 screens . 1->Fragment(inside fragmentactivity) 2->Fragment 3-> Activity. In screen 3 I call screen 1 to do something and I return again to screen 3. Now if I press back button in screen 3 I want to go to screen 2 instead of screen 1 . I tried this logic but it returned back to screen 1 instead of 2 .Any suggestions please?
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
Log.d("TAG","back button ");
finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
if you want remove activity form Activity Stack you need call finish() method or start activity with Intent.FLAG_ACTIVITY_CLEAR_TOP, as second solution remove all activity from Stack you don't need that.
in your situation you need call finish() method on following step:
1-> 3
3-> 1
use addtobackstack in your fragment transaction:
getSupportFragmentManager().beginTransaction()
.add(detailFragment, "detail")
// Add this transaction to the back stack
.addToBackStack()
.commit();
http://developer.android.com/training/implementing-navigation/temporal.html
Related
if I launch a full-screen dialog like such
FragmentManager fragmentManager = getActivity().getFragmentManager();
DialogStyleCreator editor = new DialogStyleCreator();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
transaction.add(android.R.id.content, editor).commit();
How can I make it so that if I hit the android back arrow (in the top left of the menu) or the back button it closes the dialog instead of going back to the previous activity like in alert dialogs?
You may simply put your transaction in stack of fragment manager and override the function of back button
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (Integer.parseInt(android.os.Build.VERSION.SDK) > 5
&& keyCode == KeyEvent.KEYCODE_BACK
&& event.getRepeatCount() == 0) {
// Simply pop back your fragment stack here
return true;
}
return super.onKeyDown(keyCode, event);
}
#Override
public void onBackPressed() {
// Simply pop back your fragment stack here
}
Since you have added the DialogFragment as transaction.add(android.R.id.content, editor).commit(); you may not have option to listen the dismiss call back.
I suggest you to create the DialogFragment instance and start it like
fm.beginTransaction().add(sampleDialog, "Dialog").commit();
and set the call back interface for Activity Back Button Click and call dismiss();
I am using Fragment A,Fragment B .. and so on. i want to navigate back to the previous Fragment, from the current Fragment.
I want to use Back Button in Mobile to get back navigation.
How can i do this ? OR i have to make Back button in layout xml.
i tried below code,
fTransaction.add(R.id.container, nF).addToBackStack(null).commit();
when i pressed Back Button in Mobile , it restarts the App.
To go back to previous fragment using phone's back button you should override onKeyDown() mehtod.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if(keyCode == KeyEvent.KEYCODE_BACK)
{
getFragmentManager().popBackStack();
return true;
}
return false;
}
When replacing a fragment use replace instead of add and then addToBackStack:
fTransaction.replace(R.id.container, nF).addToBackStack(null).commit();
Try this:
fTransaction.addToBackStack(null).add(R.id.container, nF).commit();
I am starting B Fragment from Fragment A.Now from Fragment B i hit Home Button.Again i open the app and it calls OnResume. Now if i hit back button it exit from the app.What should i do?
Fragment A to Fragment B Activity
Intent find = new Intent(getActivity(),FindActivityMain.class);
find.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
***startActivity(find);***Here i am not finishing the activity.
Fragment B Activity to Fragment B transaction code
Fragment myfindfragment = new FindFragmentMain(FindActivityMain.this,mylistitem,FindActivityMain.this,distance,featuredAD);
getSupportFragmentManager().beginTransaction()
.replace(R.id.blankfindlandingframe, myfindfragment).commitAllowingStateLoss();
}
Fragment A Activity is "SingleInstance" declared in Manifest.In General the Back Navigation works Perfect.But If i do these step ->
1.) Click Home Button
2.) Again long press home button to restore the app
3.) Click back button.Exit from the app.
This 3rd Step should not occur.
Please Help !!
remove find.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
add your code 'addToBackStack(null)'
Fragment myfindfragment = new FindFragmentMain(FindActivityMain.this,mylistitem,FindActivityMain.this,distance,featuredAD);
getSupportFragmentManager().beginTransaction()
.replace(R.id.blankfindlandingframe, myfindfragment).addToBackStack(null).commitAllowingStateLoss();
}
Inside Activity B add this:
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0)
{
finish();
return false;
}
return super.onKeyDown(keyCode, event);
}
I want to go back to another fragment by pressing the back button. I already read, that the addToBackStack (String tag) should help but it didn't really work.
Here is what I'm doing when switching fragments.
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right);
ft.replace(R.id.content_frame, new MainFragment());
ft.addToBackStack("Mainfragment");
ft.commit();
So now, the Fragment opens, and starts an AsyncTask where a loading circle appears. After the loading the data gets displayed. When I now press the back button once, the fragment goes back to the start of the AsyncTask where the loading circle is created. But the AsyncTask doesn't continue. When I press the back button again, the app closes.
I tried to add onBackPressed but it just told me, that this won't work in a Fragment. What would be the best way to go here?
Edit for clarification:
There's no error. It's just not working. It is like I don't even have the line addToBackStack –
I had the same situation before and I ended up with this solution. When you add or replace the Fragments, you need to add it to the backStack with a unique name. Then when the back button is pressed you can see which fragment was the active one with the method below inside the FragmentActivity that you created the Fragment.
private String getCurrentFragmentName() {
int backStackEntryCount = getSupportFragmentManager().getBackStackEntryCount();
String fragmentName;
if (backStackEntryCount > 0) {
fragmentName = getSupportFragmentManager().getBackStackEntryAt(backStackEntryCount - 1).getName();
} else {
fragmentName = "";
}
return fragmentName;
}
and in on onKeyDown() method do the following.
if (keyCode == KeyEvent.KEYCODE_BACK && getCurrentFragmentName().equals("your fragment name")) {
// Handle back press for this case.
return true;
} else if (keyCode == KeyEvent.KEYCODE_BACK
&& getCurrentFragmentName().equals("your another fragment")) {
// Handle back press for another Fragment
return true;
} else {
return super.onKeyDown(keyCode, event);
}
And this is the Place how I add the Fragment with backStack
transaction.addToBackStack("Your Fragment Name");
I want to go back to the last fragment in the back stack, so I want to make the back button popback the stack. Should I do this? and if so, should I override onBackPressed() or onKeyDown()?
#Override
public void onBackPressed()
{
Intent intent = new Intent(this,ABC.class);
startActivity(intent);
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK ) {
// do something on back.
return true;
}
return super.onKeyDown(keyCode, event);
}
My fragments aren't being added to the back stack properly for some reason
I am using this to try to go back to the previous fragment, but the order is acting strange. What exactly should I do to make the order proper?
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft){
if(teamsFrag !=null)
{
if(manage.getBackStackEntryCount() > 0)
manage.popBackStack(manage.getBackStackEntryAt(manage.getBackStackEntryCount()-1).getName(), FragmentManager.POP_BACK_STACK_INCLUSIVE);
//ft.detach(dataFrag);
}
Short answer? No.
Long answer? If you have to ask, No.
You should set your fragments up using the fragment manager so that the back button does what you want. You shouldn't override the back button instead of implementing your stack correctly.
I want to go back to the last fragment in the back stack, so I want to make the back button popback the stack.
This is the default behavior when you use FragmentTransaction#addToBackStack() while adding new Fragments with the FragmentManager.