Go back main page fragment when user click "done" - android

I'm trying to go back to main page fragment when user click to 'Done' button. It can be like swifts' "dismiss()" function. However, I dont know what am I using like that function in android.
My code run like; open activity from fragment and this fragment load detail fragment.
First of all, the first fragments' adapter open the second fragments' activity :
Intent intent = new Intent(view.getContext(), DetailActivity.class);
intent.putExtra(Consts.EXTRA_OFFER_DETAIL, binding.getController());
view.getContext().startActivity(intent);
After that, activity open fragment automatically
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.container_fragment, new DetailFragment(), null);
transaction.commit();
And I want to close last fragment when user click alert dialogs' done button
builder.setNegativeButton("Done", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
getActivity().getFragmentManager().popBackStack(); //That is what I tried but doesnt work
}
});

I'm not quite sure about your activity and fragment workflow but to close the whole activity you can just call
finish();
to dismiss the whole activity.
Since there is no fragement in the backstack, popBackStack() won't do anything.
If you only wnat to dismiss the fragement, you should use:
super.onBackPressed();

Related

Go back to a Fragment from an Activity onBackpress

I Tried the following code to move from a Fragment extends Fragment to an Activity extends activity.
Intent mIntent = new Intent(getActivity(), UserProfile.class);
Bundle bundle = new Bundle();
bundle.putBoolean(Constant.isFromChatScreen, false);
mIntent.putExtras(bundle);
startActivity(mIntent);
In BaseActivity.java I have Fragment A, Fragment B, Fragment C..from Fragment A I moved to Userprofile.java now I want to move back to Fragment A onBackPressed
How can I do that?
Check Google documentation for findFragmentByTag() here.
You can also use addToBackStack() while calling the fragment from FragmentTransaction
Just use addToBackStack(null) before commit when you make fragment transactions and then when you press the back button you will get back to your fragment.
Old question, but anyone looking for an answer to this can just do this:
#Override
public void onBackPressed() {
finish();
}
finish() "closes" the current Activity and takes you back to the Fragment (Actually, it takes you back to the Activity hosting the Fragment)

Activity to Fragment on button click

I have navigation bar on my app. Using that has created fragments. I added regular activities to the app and I have found how to go from a Fragment page to an activity page with using the onclick method.
By the click of a button I want to go from an activity page to a fragment page. How do I go about that?
Thank you
apply on click listner on the button and apply below code :-
Fragment fragment = new VehiclesFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.container,fragment).commit();
If you want to close the current activity to go back to your Fragment-container Activity you may call the finish() method or onBackPressed() method (in case you're not overriding it).
If you want to bring a fragment to the current activity you must have a fragment container and attach it with:
getSupportFragmentManager().beginTransaction.add(R.id.container, fragment, TAG).addToBackStack().commit();
Try this code..
btn.setOnClickListener(new View.OnClickListener()){
#Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(),FragmentTest.class);
getActivity().startActivity(intent);
}
});
If you want to move around in fragment then you have to be using either navigation bar clicks on tabs or use this from one fragment to another on a click of button .. like this
public void onNavigationDrawerItemSelected(int position) {
FragmentManager fragmentManager = getFragmentManager();
Fragment fragment = Fragment.instantiate(MainActivity.this, <name of the fragment>);
Bundle bundle = new Bundle();
bundle.putBoolean(ALL_EVENTS, true);
fragment.setArguments(bundle);
fragmentManager.beginTransaction().replace(R.id.container, fragment).commit();
Simply use this:
Intent(view.context, DestinationActivity::class.java).also {
view.context.startActivity(it)
}

How to avoid Fragment going back to previous Fragment on pressing device back button?

I am developing an android in which when I pressed device back button I go to previous fragment. What I want to achieve is that when I pressed device back button I don't want to go to previous fragment. How can I achieve that?.
You must be doing calling addtobackstack("name") while adding or replacing the fragment.Remove this function before calling next fragment you wont go back to previous fragment for further desc
Do not add that fragment to backstack of the new fragment you're calling. Look at the below example I've used to explain you how this works.
To make that fragment come again, just add that fragment to backstack which you want to come on back pressed,
Eg:
btnSignIn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Fragment fragment = new LoginFragment();
//replacing the fragment
if (fragment != null) {
FragmentTransaction ft = ((FragmentActivity)getContext()).getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, fragment);
ft.addToBackStack("SignupFragment");
ft.commit();
}
}
});
In the above case, I m opening a LoginFragment when signIn button is pressed, right now am in SignupFragment. So if I call addToBackStack(TAG), TAG = "SignupFragment", then when back button is pressed in LoginFragment, we come to SignUpFragment.
Happy Coding!

Issue with back button in fragment android

I have made in appliction using fragments.First there is a home sreeen in which there is a login button.when user clicks the login button a new fragment is opened which has username and password in it.I have put the loginfragment into backstack so that user can navigate to the home screen.If the user enters the correct credentials in the login frag,main fragments is opened.I have also put the mainfrag so i can navigate back and forth.But now what i want is that when the user is on home screen and if he pressed the back button the app should close but in my case it is going back to login fragment.
For eg
1] if user press login button from home screen
backstack contains "login"
2] now if user enters correct credentials and clicks ok, main frag is opened
backstack contains "main"
"login"
3] now in main frag i have 2 buttons say A and B,now if the user clicks on button A
backstack contains "A"
"main"
"login"
Now what i want is that when user clicks on back button when he is on main frag, the app should finish ,but in my case it goes back to login frag because login is still present in the backstack
EDIT -
Solution 1:
#Override
public void onClick(View view) {
Intent intent = new Intent(view.getContext(), Main.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
getActivity().finish();
}
NOTE:
I assume - Home class is your current home screen Class and Main class is your main starting point of the application.
Use Flag Intent.FLAG_ACTIVITY_CLEAR_TOP and call very First page from the Home page.
Solution 2:
If you have only one activity that handles all the fragments then just call,
#Override
public void onClick(View view) {
getActivity().finish();
}
You have to do this :
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.isTracking()
&& !event.isCanceled()) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);
return true;
}
return super.onKeyUp(keyCode, event);
}
Say if it is what you want :)
When user clicks back button, first you have get the entry from the stack. If the entry is your main fragment kill the app.
android.os.Process.killProcess(android.os.Process.myPid());
finish();
While you have only login fragment visible(only login fragment at fragment stack), if back button is clicked then you want the activity to be closed?
Solution: Add login fragment to fragment stack without adding it to backstack (without using addToBackStack() method.)
For instance:
Adding a fragment without adding it to backstack:
ExampleFragment fragment = new ExampleFragment();
fragmentTransaction.add(R.id.fragment_container, fragment);
fragmentTransaction.commit();
Adding a fragment wiht adding it to backstack:
ExampleFragment fragment = new ExampleFragment();
fragmentTransaction.add(R.id.fragment_container, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.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.
Read more about fragments.

How to hide fragment with transaction fast as possible - Android?

My current activity contains fragments. I want to open new Activity on button click from current activity. New Activity is transparent, and because of that I have to hide fragment from prev Activity. No buttons from prev activity should be shown under new Activity's buttons. I've done that. It works. Only problem is because there is little pause between switching these two Activities. Obviusly hiding fragment of prev activity takes time. Can I make it hide fragment faster? Here is my code:
#Override
public void onArcadeButtonClicked() {
fragmentManager= getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.hide(fragments[MAINMENU]);
transaction.commit();
Intent i= new Intent(this,ArcadeMapActivity.class);
startActivity(i);
}
When i remove first four lines and only call new Intent there is no pause, new Activity is shown immediately, but fragment from old Activity stays visible under new Activity. Any idea how to solve this problem?

Categories

Resources